Join us for an expert-led overview of the tools and concepts you'll need to pass exam PL-300. The first session starts on June 11th. See you there!
Get registeredPower BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.
Hi Friends,
Newbie to DAX here and I'm trying to get my head around something:
I have a calculated column that I was experimenting with. The formula is as follows:
var x = trxs[ImportDayNum] return COUNTROWS(FILTER(ALL(trxs), trxs[ImportDayNum] =x))
Without getting into the weeds of it this formula actually works as intended. My question is this:
If I remove the Var, and simply code it this way:
COUNTROWS(FILTER(ALL(trxs), trxs[ImportDayNum] =trxs[ImportDayNum])) --> It sends back a whacky result!
I'm totally confused!
If the var IS the same thing as the long-coded form, why would I get two results?
Please help!
Solved! Go to Solution.
Thanks for the reply from freginier and Jihwan_Kim , please allow me to provide another insight:
Hi, @Stivik1981
Thanks for reaching out to the Microsoft fabric community forum.
Yes, as freginier and Jihwan_Kim mentioned, when you use var x = trxs[ImportDayNum], you create a variable x that holds the ImportDayNum value of the current row. Then, you use this variable in the FILTER() function to compare all rows' ImportDayNum values to x. This means you are comparing all rows' ImportDayNum values to the ImportDayNum value of the current row.
var x = trxs[ImportDayNum]
return COUNTROWS(FILTER(ALL(trxs), trxs[ImportDayNum] = x))
This formula calculates the number of rows that have the same ImportDayNum value as the current row.
However, when you remove var and directly use trxs[ImportDayNum], the formula becomes:
COUNTROWS(FILTER(ALL(trxs), trxs[ImportDayNum] = trxs[ImportDayNum]))
In this case, trxs[ImportDayNum] = trxs[ImportDayNum] is TRUE for every row because each row's ImportDayNum value is always equal to itself. This means the FILTER() function does not filter out any rows, resulting in the count of all rows in the table.
Therefore, using var to create the variable x is crucial as it ensures that you are comparing the ImportDayNum value of the current row in the FILTER() function, rather than the ImportDayNum value of each row. Besides its use in the FILTER function, using var also improves code readability, aesthetics, and facilitates reuse.
Here are the relevant document screenshots:
For more details, please refer to:
Use variables to improve your DAX formulas - DAX | Microsoft Learn
VAR keyword (DAX) - DAX | Microsoft Learn
Of course, if you have any new discoveries or questions, please feel free to get in touch with us.
Best Regards,
Leroy Lu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Thanks for the reply from freginier and Jihwan_Kim , please allow me to provide another insight:
Hi, @Stivik1981
Thanks for reaching out to the Microsoft fabric community forum.
Yes, as freginier and Jihwan_Kim mentioned, when you use var x = trxs[ImportDayNum], you create a variable x that holds the ImportDayNum value of the current row. Then, you use this variable in the FILTER() function to compare all rows' ImportDayNum values to x. This means you are comparing all rows' ImportDayNum values to the ImportDayNum value of the current row.
var x = trxs[ImportDayNum]
return COUNTROWS(FILTER(ALL(trxs), trxs[ImportDayNum] = x))
This formula calculates the number of rows that have the same ImportDayNum value as the current row.
However, when you remove var and directly use trxs[ImportDayNum], the formula becomes:
COUNTROWS(FILTER(ALL(trxs), trxs[ImportDayNum] = trxs[ImportDayNum]))
In this case, trxs[ImportDayNum] = trxs[ImportDayNum] is TRUE for every row because each row's ImportDayNum value is always equal to itself. This means the FILTER() function does not filter out any rows, resulting in the count of all rows in the table.
Therefore, using var to create the variable x is crucial as it ensures that you are comparing the ImportDayNum value of the current row in the FILTER() function, rather than the ImportDayNum value of each row. Besides its use in the FILTER function, using var also improves code readability, aesthetics, and facilitates reuse.
Here are the relevant document screenshots:
For more details, please refer to:
Use variables to improve your DAX formulas - DAX | Microsoft Learn
VAR keyword (DAX) - DAX | Microsoft Learn
Of course, if you have any new discoveries or questions, please feel free to get in touch with us.
Best Regards,
Leroy Lu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi there!
Interesting question.. I believe this is the answer:
Hope this makes sense and that you understand a bit better.
Cheers!
Zoe 😁😁
Hi,
I think, if you want to write without VAR, please try using EARLIER DAX function.
EARLIER function (DAX) - DAX | Microsoft Learn
"Returns the current value of the specified column in an outer evaluation pass of the mentioned column.
EARLIER is useful for nested calculations where you want to use a certain value as an input and produce calculations based on that input. In Microsoft Excel, you can do such calculations only within the context of the current row; however, in DAX you can store the value of the input and then make calculation using data from the entire table.
EARLIER is mostly used in the context of calculated columns."
calculated column CC =
COUNTROWS (
FILTER ( trxs, trxs[ImportDayNum] = EARLIER ( trxs[ImportDayNum] ) )
)
If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.
Schedule a short Teams meeting to discuss your question
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
14 | |
10 | |
10 | |
9 | |
9 |
User | Count |
---|---|
19 | |
14 | |
13 | |
11 | |
8 |