DAX Variables used in formula

dicken

Active Member
Joined
Feb 12, 2022
Messages
292
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a question re variables, which i tend to use as it makes it easier to understand but in the formula I have for a running total in a calculated column
I have to use a var or it won't work;


Excel Formula:
VAR aitem = AllandValues[item]
RETURN
    CALCULATE (
        [avsales],
        FILTER (
            ALL ( AllandValues ),
            AllandValues[dex] <= EARLIER ( AllandValues[dex] )
                && CALCULATETABLE ( VALUES ( AllandValues[item] ) ) = aitem
        )
    )
the item (aitem) has to be declared as a variable otherwise you just get a running total. So what's happening?

Richard



Richard
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
If you put the column inside the FILTER function, it is inside the row context of filter. By defining it as a variable as you have, it is referenced outside of the row context of filter (but in the row context of the column). If you want to replace the variable and just use the column, you need to wrap the column inside EARLIER() to escape the filter row context.
 
Upvote 0
If you put the column inside the FILTER function, it is inside the row context of filter. By defining it as a variable as you have, it is referenced outside of the row context of filter (but in the row context of the column). If you want to replace the variable and just use the column, you need to wrap the column inside EARLIER() to escape the filter row context.
Thanks, I may have to re read that several times,
this actually came about as I made a mistake and meant to use;
 
Upvote 0
If you put the column inside the FILTER function, it is inside the row context of filter. By defining it as a variable as you have, it is referenced outside of the row context of filter (but in the row context of the column). If you want to replace the variable and just use the column, you need to wrap the column inside EARLIER() to escape the filter row context.
Thanks, I may have to re read that several times,
this actually came about as I made a mistake and meant to use;

Excel Formula:
VAR currentd = Table1[Date]
RETURN
    CALCULATE (
        [Tsales],
        FILTER ( ALL ( Table1 ), Table1[Date] <= currentd ),
        CALCULATETABLE ( VALUES ( Table1[Product] ) )
    )

One thing I have found is that if ALLEXCEPT and ALL and VALUES when used in a measure similar to the above column,
ALL and VALUES will return a running total that respects the 'Product' even if 'Product' is not present to the pivot table, so if just a date column is present , but ALLEXCEPT does not it behaves pretty much like a 'normal' running total measure,
I don't know if you can recommend anything to read most of what I find is using them to create a given denominator for percentages, I have make up a p table showing both examples so may cross post with forum, but would be interested in any comments on this behavior.

Richard.
 
Upvote 0
If you put the column inside the FILTER function, it is inside the row context of filter. By defining it as a variable as you have, it is referenced outside of the row context of filter (but in the row context of the column). If you want to replace the variable and just use the column, you need to wrap the column inside EARLIER() to escape the filter row context.
Sorry , know this is third response, I've tried wrapping column in EARLIER and works, this of course means I now have a lot more to re think EARLIER , have you seen chris webbs blog where he shows how to use it in a running total measure? I've got it copied for reference, you may understand it.
RD
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top