Forum Discussion

Sharkybu's avatar
Sharkybu
Helper II
1 year ago
Solved

Dax - comparing rows in one table based on multiple parameters.

Hello. I have a very complicated and large database that i have to do multiple comparison between rows of one table. I currently have a report build using power query but it made the report even he...
  • Sharkybu's avatar
    1 year ago

    So I found a solution.

    I dont't know if its the best- but it works.

    I created a bunch of new columns using the LOOKUPVALUE commend.

    The columns I created per situation

    example for the comparisons :

    1- rows where the tables are different and the unit and contract is the same.
    I brought the date from the rows with Table=prod in cases where the unit and contract match

    DateIn2Table= LOOKUPVALUE('Table'[Date], 'Table'[Id], 'Table'[Id], 'Table'[Unit], 'Table'[unit], 'Table'[Contract], 'Table'[Contract], 'Table'[Table] = "prod")

    and then I just added a column comparing dates between Table=test and  Table= prod

    outcome comparison 1= IF('table'[Table]= "prod", "", 
                          IF('Table'[Date]='table'[DateIn2Table], "", "Different Date"
                           ))

    2- rows where the table and contract is the same 

             I first created an index for the different units per Id and table.
             I needed to create a new column that combined the Id and table

    IdTable = 'Table'[Id] & 'Table'[Table]

    and then the index

    Index2 = RANK(DENSE, ORDERBY('Table'[Unit],ASC),PARTITIONBY('Table'[IdTable]))

    Then I got the dates in cases where the Id, Contract ant table match and the index is 1 higher

    2date = LOOKUPVALUE('Table'[Date], 'Table'[Id], 'Table'[Id], 'Table'[Contract], 'Table'[Contract], 'Table'[Table], 'Table'[Table], 'Table'[Index2], 'Table'[Index2]-1) 

    and then I compered the two dates

    outcome comparison 2= IF('Table'[Date]='table'[2Date] , "",  "Different Date")

    3- rows where the table and unit are the same and the date is the next one (the most complicated one)

        I created another index based on Id, unit, table and start date.
    First i created a new Id combining Id, unit, table.

    IdUnitTable = 'Table'[Id] & 'Table'[Unit] & 'Table'[Table]

    then the index

    Index = RANK(DENSE, ORDERBY('Table'[Date],ASC),PARTITIONBY('Table'[IdUnitTable]))

    then I got the contract from each earlier date

    Pre_Contract = LOOKUPVALUE('Table'[Contract], 'Table'[Id], 'Table'[Id], 'Table'[Unit], 'Table'[Unit], 'Table'[Table], 'Table'[Table], 'Table'[Index], 'Table'[Index]-1) 

    and last I compered the two contracts

    outcome comparison 3 = IF('Table'[Index]=1, "",
                          IF('Table'[Contract]-'Table'[Pre_Contract]=11, "", "Contracts in wrong order"
                                              ))

    The explanation is kinda messy, but I hope it will help someone in the future.