Use DAX variable withing measure - powerbi

Is it possible to use a variable within a DAX measure expression?
For example, the following measure isn't working (it always returns 0).
Notice the second variable below is referencing the first:
Measure =
VAR ThisMonth =
CALCULATE (
ABS ( SUM ( 'Table'[Saldo] ) );
FILTER ( Table; Table[Conta] = 71 )
)
VAR PreviouzMonth =
CALCULATE (
ThisMonth;
PREVIOUSMONTH ( 'Calendário'[Date] );
FILTER ( ALL ( 'Calendário'[Mês] ); MAX ( 'Calendário'[Mês] ) > 1 )
)
RETURN
ThisMonth-PreviouzMonth
But if the two variables above are calculated separetely - ie as two different measures - the calculation works fine.
Thanks for supporting!

You can have variables in expressions.
The issue is somewhere else.
Something simple like this work;
Measure =
VAR X = SUM('Sheet1 (3)'[Total])
VAR Y = DIVIDE(X,5,0)
RETURN X-Y
When you use ThisMonth inside calculate, it's not an expression. It's a variable. That might be it.

Related

DAX is not returning same value when using exact same statement, but with and without defining variables

Please refer to the syntax and table below. I'd like to use the variable, making it more efficient and readable of course, but it doesn't seem to work for me. It is the exact same statement, however I'm (not) defining the variable in the measure "testDAX" ("testDAX2").
I'm looking for the output value being 137, as given by testDAX. The 830 is simply the count of all rows in the table, without applying the filter of ProdCount = 1.
Just added the _tableD6 code. Then I copied the table in the DAX code exactly as is.
FYI: Curbal does the same thing in this video, starting at 3:00, but slightly less complex (without variables). https://www.youtube.com/watch?v=XeV0ndga5kg&t=218s
Thanks in advance!
YK
Code:
testDAX =
CALCULATE (
COUNTROWS (
SUMMARIZE ( Orders, Orders[OrderID], "ProdCount", COUNT ( Orders[ProductID] ) )
),
FILTER (
SUMMARIZE ( Orders, Orders[OrderID], "ProdCount", COUNT ( Orders[ProductID] ) ),
[ProdCount] = 1
)
)
and
testDAX2 =
VAR _tableD6 =
SUMMARIZE ( Orders, Orders[OrderID], "ProdCount", COUNT ( Orders[ProductID] ) )
RETURN
CALCULATE ( COUNTROWS ( _tableD6 ), FILTER ( _tableD6, [ProdCount] = 1 ) )

PowerBI DAX to find value of a column from another column in same table

I'm trying to match the rankorg column with a calculated measure ceiling and pull out the corresponding blackout value -
ValMaxSequence =
CALCULATE (
VALUES ( org[Blackout] ),
FILTER ( ALL ( org[rankorg] ), org[rankorg] = ( [ceiling] ) )
)
I also tried a lookup function, but it needs column value, but I have the result in a measure
The ceiling measure is being calculated within the context of the FILTER function.
Try pre-calculating it before you use it inside another function.
ValMaxSequence =
VAR Ceil = [ceiling]
RETURN
CALCULATE (
VALUES ( org[Blackout] ),
FILTER ( ALL ( org[rankorg] ), org[rankorg] = Ceil )
)

filtering measures based on two columns in power bi dax

I want to use a measure and filter the result based on the columns:
My measure is :
TotalProductionCon =
SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[SGWCP8] )
+ SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[retard] )
and I want it to summarize only when column année = column year.
I tried CALCULATE and FILTER;
TotalProductionCon =
CALCULATE (
SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[SGWCP8] )
+ SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[retard] );
FILTER (
ALL ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année] );
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année] = _BI_SOVAC_PROD_KIT_LIFE_CYCLE[year]
)
)
but it generates an error that the columns contain much value and I need to use aggregation.
Can you help me?
The problem with your formula is that you limited ALL function to only one column (Annee), and as a result FILTER does not "see" the other column it needs.
To fix that, change your formula as follows:
TotalProductionCon =
CALCULATE (
SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[SGWCP8] )
+ SUM ( _BI_SOVAC_PROD_KIT_LIFE_CYCLE[retard] );
FILTER (
ALL (
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année];
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[year]
);
_BI_SOVAC_PROD_KIT_LIFE_CYCLE[Année] = _BI_SOVAC_PROD_KIT_LIFE_CYCLE[year]
)
)
I am assuming here that your choice of ALL function is appropriate; otherwise you might need to use a different technique such as SUMMARIZE function.

Sum of a measure per date range

I have the following Table Visualization.
I'd like the table to look like the following. Column C should be averaging the range of Column B.
For example:
C2 = AVERAGE(B2:B2)
C3 = AVERAGE(B2:B3)
C4 = AVERAGE(B2:B4)
and so on.
The Year-Month column is from my MonthTable. The schema is as follows,
And the Sum measure DAX is as follows,
For the CumulativeSum measure, I have tried the following.
CumulativeSum =
CALCULATE(
[Sum],FILTER(AppendedTables,AppendedTables[Year-Month] <= MAX(AppendedTables[Year-Month]))
)
I'm guessing the issue is my CALCULATE([SUM]) area. I wanted to wrap [SUM] in a SUM() method, but that doesn't work. It gives the error "The SUM function only accepts a column reference as the argument number 1".
Please enlighten me.
I've been able to produce your desired results by creating a calculated column using the following code:
CumSum =
VAR CntRow =
COUNTROWS ( FILTER ( Sheet1, [Year-Month] >= EARLIER ( [Year-Month] ) ) )
VAR CumSum =
CALCULATE (
SUMX ( Sheet1, Sheet1[Sum] ),
FILTER ( Sheet1, Sheet1[Year-Month] >= EARLIER ( Sheet1[Year-Month] ) )
)
RETURN
DIVIDE ( CumSum, CntRow )
Hope this helps!!

DAX if else for measure

How to use if else for DAX in the measure. If row value =1 then take the var a calculated value else take the var b calculated value
x:=var a=[DATA1]
var b=[DATA2]
return(if([HOUR]=1),a,b)
I get error using above formula
It seems your problem is that you are not aggregating the columns while creating the measure. Measures only works aggregating data in a given context, generally if you want to perform calculations per row you should use a calculated column instead of a measure.
And the DAX expression for a calculated column should be:
MyColumn = IF([HOUR] = 1, [DATA1], [DATA2])
Otherwise if you want to use a measure you have to explicitely aggregate the column values in the given context, i.e:
MyMeasure =
VAR a =
FIRSTNONBLANK ( ExampleTable[Data1], 0 )
VAR b =
FIRSTNONBLANK ( ExampleTable[Data2], 0 )
RETURN
IF ( SUM ( ExampleTable[Hour] ) = 1, a, b )
Or simply:
MyMeasure =
IF (
SUM ( [Hour] ) = 1,
FIRSTNONBLANK ( ExampleTable[Data1], 0 ),
FIRSTNONBLANK ( ExampleTable[Data2], 0 )
)
Let me know if this helps.