Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
powerbi1980
Advocate I
Advocate I

DAX Calculate where column text is IN another scalar measure

I am trying to create a compare to previous period report, and having some issues with a measure to calculate amounts based on the previous periods. 

 

Very simple dashboard with one slicer on it. The slicer is for the Fiscal Period (ex. 202301, 202302, 202303 etc).  users can select multiple periods to see the selected periods amounts. I would then like to do a measure to get the previous period amounts, like this: 

 

Previous Period Amount = CALCULATE( SUM(FactGL[DisplayAmount]) , DimPeriod[Period] IN [Selected Previous Periods] /*Ex. '202201,202202,202203' */ )
 
Selected Previous Periods is a measure that does the following (returns a string containing all the periods selected in the slicer as a scalar value, converted to their previous year equivalents. So if user selects periods 202301 and 202302 in the slicer, this measure returns "202201, 202202" as a string)
 
Selected Previous Periods = CONCATENATEX ( VALUES ( DimPeriod[Period] ) , CONCATENATE(LEFT([Period], 4) - 1, RIGHT([Period],2)), ",")
 
However, I recognize that "DimPeriod[Period] IN [Selected Previous Periods] /*Ex. '202201,202202,202203' */" won't work because IN is reserved for tables. I've tried to use below (Search), but to no luck.
 
Previous Period Actual Amount = CALCULATE( SUM(FactGL[DisplayAmount]) , SEARCH( DimPeriod[Period],[Selected Previous Periods]) > 0 /*Ex. '202201,202202,202203' */ )
 
Any suggestions? At its core, I'm just trying to say in my measure filter "WHERE Column Value IN text string from scalar measure"
1 ACCEPTED SOLUTION
v-jianboli-msft
Community Support
Community Support

Hi @powerbi1980 ,

 

Based on your description, I have created a simple sample:

vjianbolimsft_0-1674626229619.png

Please try:

Previous Period Amount = 
var _a = SELECTCOLUMNS('DimPeriod',"Period",[Period]-100)
return CALCULATE(SUM(FactGL[DisplayAmount]),FILTER(ALL(DimPeriod),VALUE('DimPeriod'[Period]) IN _a))

Final output:

vjianbolimsft_1-1674626764090.png

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-jianboli-msft
Community Support
Community Support

Hi @powerbi1980 ,

 

Based on your description, I have created a simple sample:

vjianbolimsft_0-1674626229619.png

Please try:

Previous Period Amount = 
var _a = SELECTCOLUMNS('DimPeriod',"Period",[Period]-100)
return CALCULATE(SUM(FactGL[DisplayAmount]),FILTER(ALL(DimPeriod),VALUE('DimPeriod'[Period]) IN _a))

Final output:

vjianbolimsft_1-1674626764090.png

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

amitchandak
Super User
Super User

@powerbi1980 , what is basis of previous perdio month of year. Make sure you are using date table and slicer, measure and visual are using field from date table

 

you can have measure like

 

example

 

Month behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Month))
Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))

 

You want select and compare

 

How to use two Date/Period slicers

https://youtu.be/WSeZr_-MiTg

 

Time Intelligence, Part of learn Power BI https://youtu.be/cN8AO3_vmlY?t=27510

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.