cancel
Showing results for
Did you mean:

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Frequent Visitor

## Wanted help to understand DAX with selection criteria

I have the following table having customer A's monthly transactional data-

and I wanted to add an additional column in the same table using DAX or M-language like below-

the condition is that if the user selects any month on top of the report we want to list the product newly added to the customer bucket list as compared to the previous month only.

in this scenario, feb-23 is selected so it is compared to Jan-23 list of products.

in Jan-23, customer purchase ={ Bread, Butter, Bear, Chicken}

and Feb-23 having purchase={ Wine, Eggs, Bread, Butter, Chicken}

so I want to mark flag {Wine, Eggs} as 1 otherwise 0.

Thanks

1 ACCEPTED SOLUTION
Super User

Here's one way to solve it. If you always want to compare to prior month, I guess you could do this as a column too, but in doing it as a measure you have the flexibility to change the date range to 'bought in any date before selected' rather than just narrowing to the prior month.

Bought Prior Month Flag =
Var _CurrentEndOfMonth = EOMONTH(MAX(dimDates[Date]),0)
Var _PriorEndOfMonth = EOMONTH(MAX(dimDates[Date]),-1)
VAR _Product = SELECTEDVALUE(SampleData[ProductID])
VAR _PriorMonthProducts = CALCULATETABLE( VALUES(SampleData[ProductID]),FILTER(ALL(dimDates), dimDates[End of Month] = _PriorEndOfMonth))
VAR _Result =
SWITCH(TRUE(),
_Product in _PriorMonthProducts, "Yes",
"No"
)
RETURN
IF(HASONEVALUE(SampleData[Product Name]) && HASONEVALUE(dimDates[End of Month]), _Result)

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

4 REPLIES 4
Super User

@sbm_tekade  You cannot achieve this using a calculated column. Columns never reference the value of a slicer, as they are already precalculated before the slicer selection: https://excelwithallison.blogspot.com/2020/09/reporting-order-of-operations.html

You need to do this as a measure, then you can add the measure to a table visual in Power BI and use that measure as a visual level filter if you need.

Please paste your sample data in as a table that we can copy paste if you want more help on how to create that measure.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Frequent Visitor

Hello @AllisonKennedy ,

Thanks for the quick response.

Yes understood that I need to create a measure for this.

I would be really grateful if you can help me with DAX a little bit.

here is the sample data below separated with white spaces.

Month-Year Product_id Product_name Cost
Jan-23 2 Butter 110
Jan-23 4 Bear 230
Jan-23 6 Chicken 290
Feb-23 5 Wine 560
Feb-23 3 Eggs 90
Feb-23 2 Butter 100
Feb-23 6 Chicken 350
Mar-23 2 Butter 120
Mar-23 6 Chicken 310
Mar-23 4 Bear 200

Super User

Here's one way to solve it. If you always want to compare to prior month, I guess you could do this as a column too, but in doing it as a measure you have the flexibility to change the date range to 'bought in any date before selected' rather than just narrowing to the prior month.

Bought Prior Month Flag =
Var _CurrentEndOfMonth = EOMONTH(MAX(dimDates[Date]),0)
Var _PriorEndOfMonth = EOMONTH(MAX(dimDates[Date]),-1)
VAR _Product = SELECTEDVALUE(SampleData[ProductID])
VAR _PriorMonthProducts = CALCULATETABLE( VALUES(SampleData[ProductID]),FILTER(ALL(dimDates), dimDates[End of Month] = _PriorEndOfMonth))
VAR _Result =
SWITCH(TRUE(),
_Product in _PriorMonthProducts, "Yes",
"No"
)
RETURN
IF(HASONEVALUE(SampleData[Product Name]) && HASONEVALUE(dimDates[End of Month]), _Result)

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Frequent Visitor