Power BI dynamic column reference with DAX formula - dynamic

I have a Orders and ExchangeRates tables connected via a date key:
What I am trying to do is to fetch the correct currency exchange rate from ExchangeRates, which are organized in columns, with the column name matching the currency codes from Orders.
I'm basically trying to make a dynamic column reference to the EUR or JPY columns by using the matching Orders[orderCurrency] like this:
orderExchangeRate = LOOKUPVALUE(ExchangeRates[Orders[orderCurrency]],
ExchangeRates[date],Orders[date])
or:
orderExchangeRage = SELECTCOLUMNS(ExchangeRates,Orders[orderCurrency], ....)
But none of the functions accept a dynamic column reference, they expect the actual name of the column
How can I dynamically refer to the EUR and JPY columns based on Orders[orderCurrency]? Isn't there something similar to INDIRECT to make a dynamic reference in PowerBI?

As far as I know, there is no such function as INDIRECT in DAX.
You have (at least) two options:
If it's just EUR and JPY, you could create two formulas and based on the currency switch between them with IF() or SWITCH(). Like: If (Currency = "EUR", LOOKUPVALUE(EUR), LOOKUPVALUE(JPY). Pseudo code, of course.
Unpivot the EUR and JPY column in the ExchangeRate table. Then you'll have a line for each date and currency, and you can reference it as you like. Especially usefull with more currency combinations. You can Unpivot in the Query Editor, Transformation Tab.

Related

Qlik - Building a dynamic view

I have a SQL query that creates a table, and every month 2 new columns will be added for that table related to the current month.
I have tried without success to set up a flat table (visual) in Qlik that will automatically expand every month to include these table. Is there a way to do this, and i so please point me in the right direction.
You can have a look at CrossTable prefix.
This prefix allows a wide table to be converted to a long table.
So if we have data like this:
After running the following script:
CrossTable:
CrossTable(Month, Sales)
LOAD Item,
[2022-10],
[2022-11],
[2022-12],
[2023-01],
[2023-02],
[2023-03],
[2023-04]
FROM
[C:\Users\User1\Documents\SO_75447715.xlsx]
(ooxml, embedded labels, table is Sheet1);
The final data will looks like below. As you can see there are only 3 columns. All xls month columns (after Item) are now collapsed under one field - Month and all the values are collapsed under Sales column.
Having the data in this format then allows creating "normal" charts with adding Month column as dimension and use sum(Sales) as an expression.
P.S. If you dont want to manage the new columns being added then the script can be:
CrossTable(Month, Sales)
LOAD
Item,
*
FROM
...

Sum Calculation for Tax

Can you please help me on Crystal Report, where I'm trying to create a formula.
Case:
I have 2 tables, Table1 has some columns (like ItemName, ItemPrice, TaxType, TaxRate).
Table2 has Items sold with columns (like ItemName, Qty, ItemPrice, ItemTotal).
I have also linked the column in crystal report for ItemName, so that they can fetch related data.
I am looking for a formula in crystal report that can sum up tax rate * item total where tax type= VAT and TaxType= GST
My formula structure will be like:
VATSum ({table1.taxrate}*{table2.totalamount}) where table1.taxtype= 'VAT'
GSTSum ({table1.taxrate}*{table2.totalamount}) where table1.taxtype= 'GST'
Please guide.
I would really appreciate any body's efforts to solve this problem.
In Group Expert you will want to begin by creating a Group on the {table1.taxtype} field. This will ensure all of your VAT and GST items sold are grouped together. If you have other taxtype values than VAT and GST you will need to decide how to handle those taxtypes. You use Select Expert to filter them out if they are not needed for the report, or do nothing and they will all appear within additional groups on the report by their taxtype values.
You will need to then create a formula field that can be used to calculate the tax for each item sold. The name of this formula will be referenced by other formula fields in the report, so I will call this formula field itemTax in this example. The formula for this field will be:
{table1.taxrate}*{table2.totalamount};
Place the itemTax formula field into the details section of the report.
From here you have 3 options on how to calculate the sum of itemTax. You may insert a Summary Field, a Running Total Field, or another Formula Field. Any of them will work for you, but I will continue with the Formula Field option.
Create another formula field and name it totalTaxByGroup. The formula for this field will be:
Sum({#itemTax}, {table1.taxtype});
Then place this formula field in the Group Footer section of the taxtype group.

DAX % of total count if measure qualifies criteria

DAX 2013 standalone power pivot.
I have a sales table with Product and Brand columns, and Sales measure which explicitly sums up sales column.
Task in hand: I need to create 1 measure RANK which would ...
if Product is filtered expressly, then return count of Products that have higher or equal sales amount, divided by total count of products.
If it's a subtotal brand level, show the same but for brands.
My current approach is using RANK and then MAXX of rank which seems working but a no-go - slow nightmare. Excel runs out of memory.
Research: it's been a week. This is the most relevant post i found anywhere, this question here , but it's in MDX.
In my example picture, I'm showing Excel formulas with which I can get to the result. Ideally there shouldn't be any helpers, 1 formula for all.
I.E.
RANK:=IF( HASONEFILTER(PRODUCTS[PRODUCT], HELPER_PROD, HELPER_BRAND)
where HELPER_PROD part would be something like this - need to find a way to refer to "current" result in pivot table like Excel does using [#[...:
HELPER_PROD:=COUNTX(ALL(PRODUCTS), [SALES]>=[#[SALES]]) / COUNTX(ALL(PRODUCTS))
HELPER_BRAND:=COUNTX(
DISTINCT(ALL(PRODUCTS[BRAND])),
[SALES]>=[#[SALES]]) /
COUNT(DISTINCT(ALL(PRODUCTS[BRAND]))
You can use the "Earlier" function to compare with the current record.
ProductsWithHigherSales:=CALCULATE(countrows(sales),
FILTER(all(Sales),
countrows(filter(Sales,Sales[Sales]<=EARLIER(Sales[Sales])))
))
Using Earlier function in measures: can-earlier-be-used-in-dax-measures
Used workbook: Excel File

SSRS Divide Sums from Different Scopes Returns 0

I have a grouped dataset. From the parent group down, the group names are:
Company > Plant > Details. In the end, I want to be able to take the sales of each plant and see what percentage it is of the entire company's sales. Let's say the company has a total sales of $500 and the sales of "Plant A" were $100 and "Plant B" sales were $400. I would image that I would need an expression at the Plant grouping level that was like:
=Sum(Sales)/Sum(Sales, "Company")
And I would get .2 for Plant A and .8 for Plant B. But if I do that, I get 0. I am at a complete loss. Any help with this would be greatly appreciated.
Additional information:
My exact setup is a little more complex than the example I gave below, but I believe the general idea still holds the same. I have a total of 6 groups:
The circled group is the equivalent of the "Plant" Group in my example. Here is the row group in my tablix for the GroupBy group (the one with the arrow pointing to it).
The expression that is circled in the picture above is the expression in question to get my percentage (right now really just a decimal, not formatted to be a percentage yet).
=sum(Fields!ActualCurrent.Value)/sum(Fields!ActualCurrent.Value, "Company")
Fields!ActualCurrent.Value is the equivalent of "Sales" in my example above. The expression above returns 0 for all groups. But yet, if I change it to
=sum(Fields!ActualCurrent.Value)+sum(Fields!ActualCurrent.Value, "Company")
It will produce the equivalent of $600 for "Plant A" and $900 for "Plant B."
I can't seem to find how it reacts as expected when adding the two sums, but produces 0 when I divide them.
It would be useful to see where are you using that expression to determine what is wrong here, but I think you can use this guide to get your desired result.
Create a tablix like this:
Note I've added Company and Plant fields as groups. Also I've deleted details group. Right click details and select Delete group and set Delete group only option.
Now in the percentage column use the following expression:
=FORMAT(
SUM(Fields!Sales.Value,"Plant")/SUM(Fields!Sales.Value,"Company")
,"P2"
)
The sum of every plan divided by the sum of the whole company group. It is not necessary but I am using FORMAT function to format the float value returned by the expression to percentage format using two decimal places.
It should show something like this:
UPDATE: Try scoping the sum to your specific group: GroupBy
=sum(Fields!ActualCurrent.Value, "GroupBy")/sum(Fields!ActualCurrent.Value, "Company")
UPDATE 2: Format the cell to show decimal digits.
Use thiss expression:
=FORMAT(
sum(Fields!ActualCurrent.Value, "GroupBy")/sum(Fields!ActualCurrent.Value, "Company"),
"F2"
)
It will format the value returned by the expression as a float with two decimal digits.
If you want to show the value in percentage format replace F2 in the expression for P2 (Percentage format with two decimal digits.)
Let me know if this helps.

Ms ACCESS and SQL: round to two decimals through query

I use queries to calculate all kinds of supplier information (average lead time, total spend for that supplier, average price, etc.). All output is shown in listboxes in forms in Ms ACCESS.
Example of a calculated number:
How do I format the output of these queries to be rounded to two decimals? I've been playing around with the listbox settings but cannot find it there. I believe I will have to do it in the query itself, but I'm not sure how.
Query code for the above number:
SELECT Avg([Item Master].PlannedLeadTime) AS AverageLeadTime
FROM [Item Master]
WHERE ((([Item Master].DateStamp)>=[Forms]![History Supplier Tool]![List2] And ([Item Master].DateStamp)<=[Forms]![History Supplier Tool]![List3]) AND (([Item Master].SupplierName)=[Forms]![History Supplier Tool]![List1]));
Note: List1 is a listbox where the user can select a certain supplier (for which the calculations are performed) and list2 and list3 are dates the user can select (as to determine a date range for the calculations).
Access SQL has a rich function set one of which is the round function.
Example:
SELECT Round(Avg([Item Master].PlannedLeadTime),2) AS AverageLeadTime
FROM [Item Master]
WHERE (...)
Further information:
http://www.techonthenet.com/access/functions/numeric/round.php
Use ROUND() Function:
The ROUND() function is used to round a numeric field to the number of decimals specified.
SQL ROUND() Syntax:
SELECT ROUND(column_name,decimals) FROM table_name;
|Parameter |Description
________________________________________________
|column_name |Required. The field to round.
|decimals |Required. Specifies the number of decimals to be returned.
So, Your required query will be:
SELECT ROUND(AVG([Item Master].PlannedLeadTime),2) AS AverageLeadTime
FROM [Item Master]
WHERE ((([Item Master].DateStamp)>=[Forms]![History Supplier Tool]![List2]
And ([Item Master].DateStamp)<=[Forms]![History Supplier Tool]![List3]) AND (([Item Master].SupplierName)=[Forms]![History Supplier Tool]![List1]));