cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
larabraghetti
Helper II
Helper II

VAR doesn't work

Dear all,

 

I tried to use VAR in PowerBI but without success.

Here below my measure:

 

MaxTest =
VAR LVL1 =
    COUNTAX ( LIMLOGN; FILTER ( ALL ( LIMLOGN[LvlCode] ); LIMLOGN[LvlCode] = 1 ) )
VAR LVL2 =
    COUNTAX ( LIMLOGN; FILTER ( ALL ( LIMLOGN[LvlCode] ); LIMLOGN[LvlCode] = 2 ) )
VAR LVL3 =
    COUNTAX ( LIMLOGN; FILTER ( ALL ( LIMLOGN[LvlCode] ); LIMLOGN[LvlCode] = 3 ) )
VAR LVLTOT =
    COUNTAX ( LIMLOGN; ALL ( LIMLOGN[LvlCode] ) )
RETURN
    IF (
        IF ( LVL1 >= LVL2; IF ( LVL1 >= LVL3; LVL1; IF ( LVL2 >= LVL3; LVL2; LVL3 ) ) )
            < LVLTOT;
        "Y";
        "N"
    )

 

When I try to put this measure into a Pivot table I will obtain the following error:

Mdx Script (Model) (9, 24) Calculation error in the measure 'LIM LOGN' [MAXTEST]: You specified a table of values as expected while a single value.

 

I have built 4 Variables and I'd like to find the maximum between LVL1,LVL2,LVL3 

Then I need to compare the Max with LVLTOT and return "Yes" or "Not".

 

Could you help me to understand where is my error?
Thank you
Lara

1 ACCEPTED SOLUTION
CheenuSing
Community Champion
Community Champion

@larabraghetti

 

The expressions will have to be written as follows

 

VAR LVL1 =
    COUNTAX ( FILTER ( LIMLOGN, LIMLOGN[LvlCode] = 1 ) ,LIMLOGN[LvlCode] )
VAR LVL2 =
    COUNTAX (  FILTER ( LIMLOGN,LIMLOGN[LvlCode] = 2 ), LIMLOGN[LvlCode] )
VAR LVL3 =
    COUNTAX ( FILTER ( LIMLOGN, LIMLOGN[LvlCode] = 3 ) ,LIMLOGN[LvlCode] )
VAR LVLTOT =
    COUNTAX ( ALL ( LIMLOGN ) ,LIMLOGN[LvlCode] 
RETURN
    IF (
        IF ( LVL1 >= LVL2; IF ( LVL1 >= LVL3; LVL1; IF ( LVL2 >= LVL3; LVL2; LVL3 ) ) )
            < LVLTOT;
        "Y";
        "N"
    )

 

It should work.  

 

If this works please accept it as a solution and also give KUDOS

 

Cheers

 

CheenuSing

Did I answer your question? Mark my post as a solution and also give KUDOS !

Proud to be a Datanaut!

View solution in original post

5 REPLIES 5
CheenuSing
Community Champion
Community Champion

@larabraghetti

 

The expressions will have to be written as follows

 

VAR LVL1 =
    COUNTAX ( FILTER ( LIMLOGN, LIMLOGN[LvlCode] = 1 ) ,LIMLOGN[LvlCode] )
VAR LVL2 =
    COUNTAX (  FILTER ( LIMLOGN,LIMLOGN[LvlCode] = 2 ), LIMLOGN[LvlCode] )
VAR LVL3 =
    COUNTAX ( FILTER ( LIMLOGN, LIMLOGN[LvlCode] = 3 ) ,LIMLOGN[LvlCode] )
VAR LVLTOT =
    COUNTAX ( ALL ( LIMLOGN ) ,LIMLOGN[LvlCode] 
RETURN
    IF (
        IF ( LVL1 >= LVL2; IF ( LVL1 >= LVL3; LVL1; IF ( LVL2 >= LVL3; LVL2; LVL3 ) ) )
            < LVLTOT;
        "Y";
        "N"
    )

 

It should work.  

 

If this works please accept it as a solution and also give KUDOS

 

Cheers

 

CheenuSing

Did I answer your question? Mark my post as a solution and also give KUDOS !

Proud to be a Datanaut!

View solution in original post

I am trying to return all selected items from a slicer, PowerBI says "the syntax for ')' is incorrect."
 
All Selected Items =
VAR Selected_Item_Names = ALLSELECTED(AccountName_ItemName[Item Name])
return
KHorseman
Community Champion
Community Champion

Your syntax is wrong in all of those COUNTAX formulas. The syntax for COUNTAX is =COUNTAX(<table>, <expression>) but you seem to have written it as =COUNTAX(<table>, <table>). The FILTER statement should be your first argument and the second should be an expression to be evaluated on each row. So VAR LVL1 = COUNTAX( FILTER( ALL (LIMLOGN), LIMLOGN[LvlCode] = 1), <insert whatever expression you're trying to evaluate here>)

 

But if I understand the results you're trying to get, I don't think you should even be using COUNTAX in the first place. If you just want to count the rows in a filtered table you should use COUNTROWS instead of COUNTAX. COUNTAX should be used if you want to evaluate some expression on each row of a table and count the results of that expression. So if you just want a count of all rows where LIMLOGN = 1, it should be

 

VAR LVL1 = COUNTROWS(FILTER( ALL (LIMLOGN), LIMLOGN[LvlCode] = 1))





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




KGrice
Solution Sage
Solution Sage

Hi @larabraghetti. I don't see anything explicitly defined for when LVL1 < LVL2, as in:

 

IF ( LVL1 >= LVL2; IF ( LVL1 >= LVL3; LVL1; IF ( LVL2 >= LVL3; LVL2; LVL3 ) ); LVL2 )

 

I'm also not sure how this would ever evaluate to anything but "N". Isn't a part of the whole always less than the whole? If you get the count of a column where the code is only 1, and then a count of the column where the code doesn't matter at all, wouldn't the second calculation always be greater than the first? Sorry if I'm missing something.

larabraghetti
Helper II
Helper II

The error is in the variable LVLTOT:
VAR LVLTOT =
COUNTAX ( LIMLOGN; ALL ( LIMLOGN[LvlCode] ) )

But I don't understand the reason... Any suggestion?


Thank you
Lara

Helpful resources

Announcements
Power BI December 2021 Update_carousel 768x460.jpg

Check it Out!

Click here to read more about the December 2021 Updates!

User Group Leader Meeting January 768x460.png

Calling all User Group Leaders!

Don't miss the User Group Leader meetings on January, 24th & 25th, 2022.

Jan 2022 Dev Camp 768x460 copy.png

Power BI Dev Camp- January 27th, 2022

Mark your calendars and join us for our next Power BI Dev Camp!

Top Solution Authors
Top Kudoed Authors