Forum Discussion

ifat_c's avatar
ifat_c
Helper I
7 years ago
Solved

Measure - SQL to DAX, How to use

Hi

I have an SQL query that I want to convert to DAX and I don't know how to do it. can you help me? 

in the table, I have an ID field, date field, and other fields. I want to return for every ID the line in which the date is the latest (= the most current status of the ID).

 

select *
from table as t
where t.date >= All (select date from table as t1 where t.id=t1.id)

 

this is the DAX query I tried to use when I had only one ID (it works). but once I have more than one ID it needs to be altered.

show_last_date =
VAR LatestDate =
CALCULATE ( MAX ( 'table'[date] ), ALL ( 'table' ) )
RETURN
IF ( MIN ( 'table'[date] ) = LatestDate, 1, 0 )

 

also, after I have a working measure, is there a way to not show it as a field in the table? (I'm using a multi-row card which I can't minimize columns in)

thanks!

  • hi, ifat_c 

    You should use ALLEXCEPT Function as below:

    show_last_date = 
    VAR LatestDate =
    CALCULATE ( MAX ( 'table'[date] ), ALLEXCEPT('Table','Table'[ID ]))
    RETURN
    IF ( MIN ( 'table'[date] ) = LatestDate, 1, 0 )

    And just drag the measrue into Visual level filter of the visual not into Value and set is "1"

     

    here is pbix file, please try it.

     

    Best Regards,

    Lin

     

5 Replies

    • ifat_c's avatar
      ifat_c
      Helper I

      Hi, I'll try to be more specific.

      in the table, I have an ID field, date field, and other fields. I want to return for every ID the line in which the date is the latest (= the most current status of the ID).

       

      this is the SQL query I would use to do this:

      select *
      from table as t
      where t.date >= All (select date from table as t1 where t.id=t1.id)

       

      I need your help to translate it to DAX to use as a measure

      sample data:

      ID Datenamestatus
      101/01/2019aon
      201/01/2019boff
      301/02/2019con
      201/02/2019bon
      301/01/2019con
      101/02/2019aoff

       

      desired outcome:

      ID Datenamestatus
      301/02/2019con
      201/02/2019bon
      101/02/2019aoff
      • v-lili6-msft's avatar
        v-lili6-msft
        Community Support

        hi, ifat_c 

        You should use ALLEXCEPT Function as below:

        show_last_date = 
        VAR LatestDate =
        CALCULATE ( MAX ( 'table'[date] ), ALLEXCEPT('Table','Table'[ID ]))
        RETURN
        IF ( MIN ( 'table'[date] ) = LatestDate, 1, 0 )

        And just drag the measrue into Visual level filter of the visual not into Value and set is "1"

         

        here is pbix file, please try it.

         

        Best Regards,

        Lin