Using IN operator in DAX with Power Bi Desktop chart? - powerbi

I'm new to DAX so please bear with me.
Can I use the IN operator in DAX to create a query directly in Power Bi desktop?
For example, the screenshot below displays over 120 curencies in DimCurrency.CurrencyName. Is it possible to create a DAX query where I can include all my filters using IN operator?
For example, maybe something like this?
CALCULATE (
[Sales Amount],
Products[Color] IN { "Red", "Black" }
)
The reason I'm interested in using DAX is because my current filter has over 200 different items, so I don't want to scroll through the 200-item list and select 5 different items.

This is a data modeling problem. In Dimensional Modeling terms your Dimension needs some additional attribute hierarchies to drive the filtering. This is just like why a Calendar table doen't just have a Day column, it needs Month, Year, Quarter, so you don't have to select all the individual Days.
Basically DimCurrency needs an additional column, so that you can include those 50 currencies by selecting fewer values of that new column.
One way to modify your data model is with DAX calculated columns, with an expression of the form
IsFacoriteCurrency = 'DimCurrency'[CurrencyName] in {"Algerian Dinar","Argentine Peso"}
you can also modify the data model in the data source or in Power Query.

Related

How do I get my rank column to refresh as I filter my other data columns?

Hello Stack Overflow community -- I am looking for a solution on how to filter my table on the REPORT tab (i.e. I want to avoid using the "Transform Data" function) and have my rank column on my report tab update as I make filter adjustments.
Right now, for example, when I filter out specific products from my product column, the corresponding rank column does not update along with it.
Visual example:
product
rank
apples
1
oranges
2
What I want to happen is if I filter out "apples" from my PRODUCT COLUMN, I want the RANK COLUMN to show "oranges" as rank #1. Now, when I filter out "apples", the rank column shifts all my data up, but oranges and the data that follows is stuck with its old rank
EDIT: I also want to know if I can filter my product column and have other columns dynamically update/refresh as well, such as Cumulative %, Market Share, and so on -- things that will require updates if the input data shifts.
Thank you!
Tried creating filters on the report tab, but columns like rank, cumulative %, market share, etc do not refresh with new rankings etc
PowerQuery columns and DAX calculated columns are updated on data refresh only. If you want the report to react on slicers/filters, you need to use measures, like
Rank Measure =
RANKX(
ALLSELECTED( example ),
CALCULATE( MIN( example[product] ) ),
,
ASC,
DENSE
)
This is also the reason why all data analysis should be done in DAX, not PowerQuery. So, you'll have to rebuild your Cumulative % or Market Share calculation as DAX measures, if you want to see the impact of filters and especially cross-filters, which is the very power of Power BI.
Accept this answer, if it was helpful to solve your problem. If not, comment why!

Dynamic filtering based on selected value in slicer in Power BI

I am trying to create a calculated table where the data is being taken from another table and calculating the average based on the username, total average and variance between the 2 of these columns.
To create a table, I used the below DAX in Power BI which calculated the average based on the username.
scanner_speed_average_calculation =
SUMMARIZE(scanner_speed
,scanner_speed[user_name]
,"Average"
,AVERAGE(scanner_speed[order_processed]))
To calculate the group_average I used the below DAX:
group_average =
SUMMARIZE(
scanner_speed
, "Group Avg"
, average(scanner_speed[order_processed]))
And finally to calculate the variance, I used this query:
Variance = scanner_speed_average_calculation[Average] - scanner_speed_average_calculation[group_average]
Below is an outcome of these calculations.
I want to be able to make these calculations dynamic based on the selected value from the date. The table where I am taking these calculations do have the date value. I want to be able to use date range in slicer and I want these values to change based on the selected date range. I tried few things with Filter, Selectedvalue but I am not sure if I used them correctly.
Below is a main table where I took all these calculations from.
Below is a visual of where I want to group_average and variance. I want to be able to use date range and these columns should change accordingly.
Any idea or help will be appreciated. If possible then please put the entire formula. I am still a newbie in the world of DAX. Thanks in advance
power bi file
If you want a calculation to depend on a slicer, you need a Measure, not a calculated column or calculated table. Calculated columns and calculated tables are generated on refresh and physically stored in your model, so the slicers can filter them, but the slicers can't change the value of the calculations.
Measures are not persisted, but are calculated as needed based on changes to filters and slicers.
If you simply add add a measure
AverageOrdersProcessed := AVERAGE(scanner_speed[order_processed])
and put that on a visual that groups by user_name, you will get a the AVERAGE(scanner_speed[order_processed]) for each `user_name'.

Filter PowerBI Measure Based on Column

I am trying to set up a dashboard with data that looks similar to this:
As you can guess, all of the 'rate' columns are related. I know to create a graphic that shows the totals for all 'rates' I first need to create a measure that is the sum of these 4 columns. My question is, how do I filter that total, based on the columns in the measure?
For example, if I had a line graph for the 'totals' measure:
Is there any way to create a filter visual that would allow me to see just the AA rates on this graph (that is, only measure the sum of the AA rates) or just the UA rates, etc.? Or is there a way to put all 4 columns on the same graph...
and then create a filter visual so single out one of the values fields?

Power BI - DAX query to remove duplicated rows in SUM calculation

My scenario is this: SalesValue have been entered for multiple sessions namely Lunch, Breakfast, dinner which is grouped by SessionKey in numbers. The same SalesValue repeats at times for 2 or more sessions for a given production plan date, based on MenuKey, RawMaterialKey and IngSFKey.
I need to use DAX query in Power BI to remove duplicated SalesValue based on ProductionPlanDate and SessionKey for a particular MenuKey in a given date.
I have attached the screenshot of a sample value range of SalesValue containing duplicate values for the same date across different sessions for your reference. For example, rows 7 and 14 have the same ProductionPlanDate, SessionKey, MenuKey, and SalesValue.
So you have a table with one "Grain" and you want to change the "Grain" by using a subset of the columns. Specifically you want only rows with distinct columns ProductionPlanDate, SessionKey, MenuKey and SalesValue
To do this in a DAX query you would use
evaluate
summarize
( 'table name'
, 'table name'[ProductionPlanDate]
, 'table name'[SessionKey]
, 'table name'[MenuKey]
, 'table name'[SalesValue]
)
You could provide this to create a calculated table or provide it to each measure that needs to work with this coarser grained data set.
However as it seems you are in Power BI the more appropriate place to do this would be to create your coarser grained table using Power Query (via the Edit Queries section of Power BI).
This is better than doing it in DAX as DAX is more tuned to analytics where Power Query is tuned to data transformation - and you want to do data transformation.
You can either keep the table that you have now alongside the new modified or replace it accordingly.
option A will just change your incoming table to have the new coarse grain.
option B will keep your original table and have the new grained table alongside it. Note that this will mean any Power BI visuals that you have created will need to be "rewired" to use the new table.
To do the transform in Power Query, the steps for both options are
Go to the Edit Queries area on PowerBI
Select the columns that you want to create the new Grain (i.e. ProductionPlanDate, SessionKey, MenuKey and SalesValue) by holding ctrl and clicking the column headers of each column in turn.
Right click on the column header for one of the selected columns and select "Remove Duplicates"
If you want option B, simply first copy the existing table by using "Reference" then do the same thing as follows:
Find your existing table on the left Queries section, r-click and click Reference
Rename the new table something appropriate
Apply the transform steps to the new table as above
Click Close & Apply and rewire any existing visuals that you need to use the new table
If you find you don't need your old table you can R-click on it in Power Query again and uncheck "Enable Load" so that PowerBI will not see it anymore.

Based on slicer selection create dynamic calculated table in Power BI

I’m new to Power BI. Currently facing similar issue explained below in my product development.
I have created power bi modle with below dimensions and facts from adventureworksDW.
Then I created a calculated table, which gives result as sum of sales group by ProductSubCategory and ProductCategory. Below is the DAX for the calculated table.
Now I want to create a new calculated table, which gives me TOPn ProductSubCategory based on the Total sales amount.
Below is the DAX to do this.
and model relationships looks like below.
I want this TOPn rows to be displayed based on filter condition on product category. Something like below.
This works fine when I hardcode the product category value in the DAX itself. But if I want to change this product category values from the slicer selection, then I didn’t get any results.
What you are asking for is not possible as Power BI is currently designed. Slicers cannot affect calculated tables. Calculated columns and calculated tables are evaluated once when the data is first loaded and are static until the data is refreshed.
However, you can get the table visual you want in a much simpler manner by writing the appropriate measure and putting that in the table instead of defining an entirely separate table.
TotalSales = SUM(FactInternetSales[SalesAmount])
The Top N filtering is available in the visual level filters settings.
You can simply use the SELECTEDVALUE function as shown below.
var __SelectedValue = SELECTEDVALUE('ProductSales'[EnglishProductCatogaryName])
return
Filter(
'ProductSales',
'ProductSales'[EnglishProductCatogaryName] = __SelectedValue
)
)