cancel
Showing results for
Did you mean:

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a \$100 discount. Register Now

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)

Please @mention me in your reply if you want a response.

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.
If you found this post helpful, please give Kudos C

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.

Please @mention me in your reply if you want a response.

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.
If you found this post helpful, please give Kudos C

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 1 Bread 40
Jan-23 2 Butter 110
Jan-23 4 Bear 230
Jan-23 6 Chicken 290
Feb-23 5 Wine 560
Feb-23 1 Bread 45
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
Mar-23 1 Bread 35

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)

Please @mention me in your reply if you want a response.

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.
If you found this post helpful, please give Kudos C

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

Frequent Visitor

Announcements

#### Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

#### Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

#### Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

#### Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors