Starting December 3, join live sessions with database experts and the Microsoft product team to learn just how easy it is to get started
Learn moreGet certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now
I have a measure called MAX DATE. It is simply: MAX DATE = MAX(Dates Table[Date])
I then wanted to use that in the following two DAX measures to see what the results would be:
Solved! Go to Solution.
Hi @rmcgrath ,
The error you’re encountering comes down to how DAX interprets scalar and table values within FILTER. Let’s break down the issues and the differences between the two formulas if they were valid.
In DAX, the FILTER function expects its second argument (the condition) to evaluate to a scalar (single value) for each row in the table context. Here’s how this causes issues in your formulas:
In short, DAX doesn’t know how to handle this mixed comparison of columns and measures within FILTER.
To filter dates up to the maximum date across all rows:
Test1 = CALCULATE(
MAX('Dates Table'[Date]),
FILTER(
ALL('Dates Table'),
'Dates Table'[Date] <= [Max Date]
)
)
To dynamically apply a date filter using the row context, you could replace MAX(Dates Table[Date]) with a variable that represents the maximum date dynamically within the desired context:
Test2 =
VAR MaxRowDate = MAX('Dates Table'[Date])
RETURN
CALCULATE(
MAX('Dates Table'[Date]),
FILTER(
ALL('Dates Table'),
'Dates Table'[Date] <= MaxRowDate
)
)
These alternatives should avoid the scalar error and give the desired filtered results.
Best regards,
1. Why the error?
There is a problem in your measure. A measure result should be a scalar value, either numeric or text. But it cannot return a table.
FILTER function is a table function, which means it returns a table not a scalar value. So you want to use Measures then one option that you have is to use COUNTROWS function.
1) Test1 = COUNTROWS(FILTER(ALL('Dates Table'),'Dates Table'[Date]<=[Max Date]))
2) Test2 = COUNTROWS( FILTER(ALL('Dates Table'),'Dates Table'[Date]<=MAX(Dates Table[Date]))
What would be the difference between the two (if they worked)?
If you want to see the results of your filter functions then you need to create tables. Create two tables, Test1 and Test2 and observe the results. The difference would be more evident if you put < rather than <=.
In Test1 you used a measure [MAX DATE] and kept it insider a filter function which applies row context, and because [MAX DATE] is a measure, row context will be transitioned into filter context. (context transition).
so for <= you will see all records in the date table.
for <, you result table will be empty.
In Test2 there wont be anny context transition, so
for <= you will see all the records
for < you will see all the records except the record with maximum date. I create the code snippet in DAX.DO and you can test it there
https://dax.do/0mNnb3xQobMy0s/
I would suggest you to go though this blog read about context transition
https://www.sqlbi.com/articles/understanding-context-transition-in-dax/
Need a Power BI Consultation? Hire me on Upwork
Connect on LinkedIn
|
Hi @rmcgrath
Measure always return scalar value. But your formula used a filter function which returns a table. So without any aggregation, if you use this , you will get such error.
You know that filter works row wise, that means, return all the rows from the specified table which meets the given condition. In your case, condition will return all the rows which less than max date.
So to fix, use aggregation function such as countrows. This function will give you result, how many dates are less than max dates.
Hope this clearify your thoghts!!
If this, please accept it as a solution and a kudos!!
Best Regards,
Shahariar Hafiz
Hi @rmcgrath ,
The error you’re encountering comes down to how DAX interprets scalar and table values within FILTER. Let’s break down the issues and the differences between the two formulas if they were valid.
In DAX, the FILTER function expects its second argument (the condition) to evaluate to a scalar (single value) for each row in the table context. Here’s how this causes issues in your formulas:
In short, DAX doesn’t know how to handle this mixed comparison of columns and measures within FILTER.
To filter dates up to the maximum date across all rows:
Test1 = CALCULATE(
MAX('Dates Table'[Date]),
FILTER(
ALL('Dates Table'),
'Dates Table'[Date] <= [Max Date]
)
)
To dynamically apply a date filter using the row context, you could replace MAX(Dates Table[Date]) with a variable that represents the maximum date dynamically within the desired context:
Test2 =
VAR MaxRowDate = MAX('Dates Table'[Date])
RETURN
CALCULATE(
MAX('Dates Table'[Date]),
FILTER(
ALL('Dates Table'),
'Dates Table'[Date] <= MaxRowDate
)
)
These alternatives should avoid the scalar error and give the desired filtered results.
Best regards,
Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.
User | Count |
---|---|
94 | |
86 | |
82 | |
70 | |
49 |
User | Count |
---|---|
143 | |
123 | |
107 | |
61 | |
55 |