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
SonyT01
Regular Visitor

How to write an IF measure when using different time periods

Hi,

 

Please can someone show me how to write the following IF measure. 

 

I have an example where I have created a table in Power BI that looks similar to the below.  I have calculated a 'YTD' field and a 'YTD' Mar 20 field.  The table has a date slicer that is selecting August 2020.

 

I would like to create the 3rd column in the table to look like 'YTD New'.  However I can't seem to work this out.  The logic of the measure I want is:

 

YTD New = If Name = "Cash" then 'YTD Mar 20' otherwise 'YTD' 

 

NameYTDYTD Mar 20YTD New
Receipts from customers400350400
Payments to suppliers300250300
Interest paid502550
Cash1004040
 

The other 2 columns are calculated by:

Measure:
YTD Mar 2020 = CALCULATE(SUM('Table1'[Balance]),DATESBETWEEN(
Dates[Date],
DATE(2020,3,1),
DATE(2020,3,31)
)
)

 

Calculated column:

YTD = ('Table1'[YTD Debit] - 'Table1'[YTD Credit])
 
I do hope this is achievable somehow.
3 ACCEPTED SOLUTIONS
DataInsights
Super User
Super User

@SonyT01,

 

Try this measure:

 

YTD New = IF ( MAX ( Table1[Name] ) = "Cash", [YTD Mar 2020], MAX ( Table1[YTD] ) )

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

v-yuaj-msft
Community Support
Community Support

Hi @SonyT01 ,

 

According to your description and my previous experience, I guess you seem to mix measures and calculated columns. It does not meet the definition rules of DAX functions.

This error can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.

Result:

v-yuaj-msft_0-1604025209007.png

 

I think you can modify the "YTD NEW" measure appropriately as follows.

 

YTD NEW =

                  var min_value=MIN('Table 1'[Name])

                  return

                 IF(

                        min_value="cash",

                        'Table 1'[YTD Mar 2020],

                       MIN('Table 1'[YTD])

                     )

 

Result:

v-yuaj-msft_1-1604025209010.png

 

I hope my suggestion can give you some help.

 

Best Regards,

Yuna

 

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

Thank you so much!!  I have been trying to figure this out with no success.  This has achieved the result I was after.

 

Many thanks again! 

View solution in original post

4 REPLIES 4
v-yuaj-msft
Community Support
Community Support

Hi @SonyT01 ,

 

According to your description and my previous experience, I guess you seem to mix measures and calculated columns. It does not meet the definition rules of DAX functions.

This error can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.

Result:

v-yuaj-msft_0-1604025209007.png

 

I think you can modify the "YTD NEW" measure appropriately as follows.

 

YTD NEW =

                  var min_value=MIN('Table 1'[Name])

                  return

                 IF(

                        min_value="cash",

                        'Table 1'[YTD Mar 2020],

                       MIN('Table 1'[YTD])

                     )

 

Result:

v-yuaj-msft_1-1604025209010.png

 

I hope my suggestion can give you some help.

 

Best Regards,

Yuna

 

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

 

Thank you so much!!  I have been trying to figure this out with no success.  This has achieved the result I was after.

 

Many thanks again! 

DataInsights
Super User
Super User

@SonyT01,

 

Try this measure:

 

YTD New = IF ( MAX ( Table1[Name] ) = "Cash", [YTD Mar 2020], MAX ( Table1[YTD] ) )

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Thank you for your help. It did work well for each category, but not for the total amount.

I was trying to calculate a % of accomplishment for each category, but the Total % achievement it just sums each category, it doesn't calculate the correct amount.


Do you know how can i fix it?

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.