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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

calculate function not giving correct answer

I have a VERY simple formula:

 

MaxMonth = calculate(max('Fact Table'[CurrentMonth]), filter('Fact Table', max('Fact Table'[RefNum])))
 
I need it to find the max "RefNum" and lookup the value in the "CurrentMonth" column.
-CurrentMonth is a whole number based off the date pulled out of a SQL query
-RefNum is a whole number that starts at the earliest date and adds 1 every month there's new data.
 
What I need is to find the latest month of data in number format.
So the date is currently Feb-19-2019, which means my SQL query pulls data upto and including Jan-31-2019. This makes my max(RefNum) = 23 (as my dataset started in March-2017) and CurrentMonth = 1.
So the formula above... should it not filter my fact table for RefNum = 23, then give me 1 (since that's the only value in CurrentMonth when RefNum = 23)?
2 ACCEPTED SOLUTIONS

If you just want to lookup the value for the max RefNum do you need to a max of the CurrentMonth column or could you just use the LookupValue function?

 

MaxMonth = LookupValue(,'Fact Table', 'Fact Table'[CurrentMonth]), max('Fact Table'[RefNum]))

Or if you have mulitple rows with the same RefNum you might need to do something like the following:

 

MaxMonth =
VAR _maxRefNum = max('Fact Table'[RefNum])
RETURN CALCULATE( MAX( 'Fact Table'[CurrentMonth]), 'Fact Table'[RefNum] = _maxRefNum )

View solution in original post

v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @Anonymous ,

 

You could display the returned result of below measure in a card visual.

MonNo for max refNo =
VAR Curr_Date =
    TODAY ()
VAR Last_day_of_currMon =
    EOMONTH ( Curr_Date, 0 )
RETURN
    IF (
        Curr_Date < Last_day_of_currMon,
        MONTH ( EOMONTH ( Curr_Date, -1 ) ),
        MONTH ( Last_day_of_currMon )
    )

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
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

5 REPLIES 5
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @Anonymous ,

 

You could display the returned result of below measure in a card visual.

MonNo for max refNo =
VAR Curr_Date =
    TODAY ()
VAR Last_day_of_currMon =
    EOMONTH ( Curr_Date, 0 )
RETURN
    IF (
        Curr_Date < Last_day_of_currMon,
        MONTH ( EOMONTH ( Curr_Date, -1 ) ),
        MONTH ( Last_day_of_currMon )
    )

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
HotChilli
Super User
Super User

The second parameter on your FILTER is not the expression you want, it should be(quote)

- 'A Boolean expression that is to be evaluated for each row of the table. For example, [Amount] > 0 or [Region] = "France" '

If you just want to lookup the value for the max RefNum do you need to a max of the CurrentMonth column or could you just use the LookupValue function?

 

MaxMonth = LookupValue(,'Fact Table', 'Fact Table'[CurrentMonth]), max('Fact Table'[RefNum]))

Or if you have mulitple rows with the same RefNum you might need to do something like the following:

 

MaxMonth =
VAR _maxRefNum = max('Fact Table'[RefNum])
RETURN CALCULATE( MAX( 'Fact Table'[CurrentMonth]), 'Fact Table'[RefNum] = _maxRefNum )

Anonymous
Not applicable

The lookupvalue function worked perfectly, THANK YOU!  I'm still a little unsure as to why the calculate function doesn't work though...


@Anonymous wrote:

The lookupvalue function worked perfectly, THANK YOU!  I'm still a little unsure as to why the calculate function doesn't work though...


The problem with the original calculate is the call to FILTER(). Filter expects 2 parameters, a table to iterate over and a boolean expression, but your call to it looked like the following

 

 filter('Fact Table', max('Fact Table'[RefNum]))

 

With a table and a numeric expression. What happens in this case is that the forumla engine attempts to do an implied cast on max('Fact Table'[RefNum]) to convert the number to a boolean value. And the way it does this is to treat values of 0 as false and non-zero as true. So this will effectively not filter any rows.

 

You should be able to fix this by comparing the current value of the [RefNum] with the max value by adding the following bold code.

 

MaxMonth = calculate(max('Fact Table'[CurrentMonth]), filter('Fact Table', 'Fact Table'[RefNum] = max('Fact Table'[RefNum])))

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.