Forum Discussion

oscargushiken's avatar
oscargushiken
Frequent Visitor
3 years ago
Solved

Conditional Merge Queries based on column value

Dear forum,

 

Would would be the best way to merge data from a bottom row based on meeting a specific column value?

I was attmepting to do this using two offset Index columns and then execute a normal Merge Queries.  However, I am at a loss as to how to make this conditionally based on matching the EMPLOYEE_IDs.

The end product shoule be rows of data where the current and the previous (bottom row) Job_Code are on the same line for each Employee_ID.  If I do a normal Merge Queries without filtering for a matching Employee_ID, then at somepoint I will be bringing up Job_Codes that don't belong to the employee in question.  I would rather populate "0" or null if there is no match.

 

BEFORE

DATEEMPLOYEE_IDJOB_CODEIndexIndex.1
2/1/202186753092902000301
11/15/201986753092902000212
3/21/201377793112903000423
5/11/200577793112903000334
3/24/200260608422901000245
2/1/200160608422901000156

 

AFTER (DESIRED)

DATEEMPLOYEE_IDJOB_CODEIndexIndex.1PREV_JOB_CODE
2/1/20218675309290200030129020002
11/15/201986753092902000212null
3/21/20137779311290300042329030003
5/11/200577793112903000334null
3/24/20026060842290100024529010001
2/1/200160608422901000156 

 

Your help in this would be amazing!

 

Thanks!

  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi oscargushiken ,

    I have created a simple sample, please refer to it to see if it helps you.

    Create a measure.

    result =
    CALCULATE (
        MAX ( 'date'[JOB_CODE] ),
        FILTER (
            ALL ( 'date' ),
            'date'[EMPLOYEE_ID] = SELECTEDVALUE ( 'date'[EMPLOYEE_ID] )
                && 'date'[Index]
                    = SELECTEDVALUE ( 'date'[Index] ) + 1
        )
    )
    

    Or a column.

    Column =
    CALCULATE (
        MAX ( 'date'[JOB_CODE] ),
        FILTER (
            ALL ( 'date' ),
            'date'[EMPLOYEE_ID] = EARLIER ( 'date'[EMPLOYEE_ID] )
                && 'date'[Index]
                    = EARLIER ( 'date'[Index] ) + 1
        )
    )
    

    How to Get Your Question Answered Quickly 

     

    If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

     

    Best Regards
    Community Support Team _ Polly

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

  • Hi,

    This calculated column formula works

    Column = LOOKUPVALUE(Data[JOB_CODE],Data[DATE],CALCULATE(MAX(Data[DATE]),FILTER(Data,Data[EMPLOYEE_ID]=EARLIER(Data[EMPLOYEE_ID])&&Data[DATE]<EARLIER(Data[DATE]))),Data[EMPLOYEE_ID],Data[EMPLOYEE_ID])

    Hope this helps.

5 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi oscargushiken ,

    I have created a simple sample, please refer to it to see if it helps you.

    Create a measure.

    result =
    CALCULATE (
        MAX ( 'date'[JOB_CODE] ),
        FILTER (
            ALL ( 'date' ),
            'date'[EMPLOYEE_ID] = SELECTEDVALUE ( 'date'[EMPLOYEE_ID] )
                && 'date'[Index]
                    = SELECTEDVALUE ( 'date'[Index] ) + 1
        )
    )
    

    Or a column.

    Column =
    CALCULATE (
        MAX ( 'date'[JOB_CODE] ),
        FILTER (
            ALL ( 'date' ),
            'date'[EMPLOYEE_ID] = EARLIER ( 'date'[EMPLOYEE_ID] )
                && 'date'[Index]
                    = EARLIER ( 'date'[Index] ) + 1
        )
    )
    

    How to Get Your Question Answered Quickly 

     

    If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

     

    Best Regards
    Community Support Team _ Polly

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

    • oscargushiken's avatar
      oscargushiken
      Frequent Visitor

      Thanks v-polly-msft, the .pbix file helps a lot and works like a charm.

  • Hi,

    This calculated column formula works

    Column = LOOKUPVALUE(Data[JOB_CODE],Data[DATE],CALCULATE(MAX(Data[DATE]),FILTER(Data,Data[EMPLOYEE_ID]=EARLIER(Data[EMPLOYEE_ID])&&Data[DATE]<EARLIER(Data[DATE]))),Data[EMPLOYEE_ID],Data[EMPLOYEE_ID])

    Hope this helps.

    • oscargushiken's avatar
      oscargushiken
      Frequent Visitor

      Thank you Ashish - this solution worked.  I was so fixated on using Power Query, that I was not considering DAX.