Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Dasbas
Frequent Visitor

DAX equivalent to this.var

To compare the date of a specific row to the rest of the dates in that column, in a calculated column. I need some kind of this.var function as we know from Java

 

Thanks!

1 ACCEPTED SOLUTION
v-ljerr-msft
Employee
Employee

@Dasbas

 

According to your description, you should be able to use VAR function to store the current row date as a named variable, then use FILTER function to iterate through all the other values of that column and compare them to the stored variable. See my sample below.

 

Assume we have table called "MyTestTable" like below, now we need to create a calculate column to compare the date of current row to the rest of dates in that column and calculate the count of them which is smaller than the current row date.

t1.PNG

Then you should be able to use the formula below to create the calculate column.

Column = 
VAR currentRowDate = MyTestTable[Date]
RETURN
    CALCULATE (
        COUNTA ( MyTestTable[Date] ),
        FILTER ( ALL ( MyTestTable ), MyTestTable[Date] < currentRowDate )
    )

cc.PNG

 

Regards

View solution in original post

5 REPLIES 5
v-ljerr-msft
Employee
Employee

@Dasbas

 

According to your description, you should be able to use VAR function to store the current row date as a named variable, then use FILTER function to iterate through all the other values of that column and compare them to the stored variable. See my sample below.

 

Assume we have table called "MyTestTable" like below, now we need to create a calculate column to compare the date of current row to the rest of dates in that column and calculate the count of them which is smaller than the current row date.

t1.PNG

Then you should be able to use the formula below to create the calculate column.

Column = 
VAR currentRowDate = MyTestTable[Date]
RETURN
    CALCULATE (
        COUNTA ( MyTestTable[Date] ),
        FILTER ( ALL ( MyTestTable ), MyTestTable[Date] < currentRowDate )
    )

cc.PNG

 

Regards

@v-ljerr-msft Thanks man, the var thing worked. Based on your answer I was able to write the querry i needed.

 

ActiveAtTime = 
VAR currentRowDate = Calls[Closure Date]
RETURN
CALCULATE(
COUNTA(Calls[Call Date]);
FILTER(ALL(Calls);Calls[Closure Date] > currentRowDate);
FILTER(ALL(Calls);Calls[Call Date] < currentRowDate)
)

Thanks a lot!

ankitpatira
Community Champion
Community Champion

@Dasbas You can add Index column to your table and then use below code to compare specific row date to the rest of the column. 

 

Index,Date

1,01/01/2016

2,02/01/2016

3,03/01/2016

4,04/01/2016

5,05/01/2016

6,06/01/2016

 

First create measure that gives you specific row date (as example getting date of row with index value of 5).

Measure = LOOKUPVALUE(TABLENAME[Date],TABLENAME[Index],5)

 

Then create calculated column as below,

Column = IF(TABLENAME[Date] = [Measure],"matched","unmatched")

@ankitpatira Thank you, that's a great concept I didn't think about. I however can't use a measure with predefined index value since i need the check executed for each row, like in a loop.

 

So to clarify with pseudo code:

int index = 0;

foreach dates {

Measure = LOOKUPVALUE(TABLENAME[Date],TABLENAME[Index],index)

 Column = IF(TABLENAME[Date] = [Measure],"matched","unmatched")

index ++;

}

You have been able to figure out the result should look something like that:

Column = IF(TABLENAME[Date] = SomeExpression ,"matched","unmatched")

 

The question is how you define SomeExpression.

 

Can you tell us more about how do you want to choose the other row? Can you show us some mockup data and what the results should look like?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.