Forum Discussion

EaglesTony's avatar
EaglesTony
Icon for Post Prodigy rankPost Prodigy
1 year ago
Solved

How do I find the correct column based off a date

I have the following table:   Based off this I have another table(i.e. table2): Key    StartDate    EndDate ABC    1/3/2024   4/1/2024 DEF     2/1/2024   4/1/2024 HIJ      4/4/2025   7/1/...
  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi EaglesTony 

     

    Please try this:

    First of all, add 2 calculated columns in the Table 2:

    PIBasedOnStartDate =
    VAR _vtable =
        FILTER (
            CROSSJOIN (
                SELECTCOLUMNS ( 'Table 2', "_StartDate", 'Table 2'[StartDate] ),
                'Table'
            ),
            [_StartDate] >= 'Table'[StartDate]
                && [_StartDate] <= 'Table'[EndDate]
        )
    RETURN
        MAXX ( FILTER ( _vtable, [_StartDate] = 'Table 2'[StartDate] ), [PI] )
    
    PIBasedOnStartDate =
    VAR _vtable =
        FILTER (
            CROSSJOIN (
                SELECTCOLUMNS ( 'Table 2', "_StartDate", 'Table 2'[StartDate] ),
                'Table'
            ),
            [_StartDate] >= 'Table'[StartDate]
                && [_StartDate] <= 'Table'[EndDate]
        )
    RETURN
        MAXX ( FILTER ( _vtable, [_StartDate] = 'Table 2'[StartDate] ), [PI] )
    

    The result:

    Then add a Calculated table:

    Outcome =
    SUMMARIZE (
        UNION (
            SELECTCOLUMNS (
                'Table 2',
                "Key", 'Table 2'[Key],
                "PIFallsIn", 'Table 2'[PIBasedOnStartDate]
            ),
            SELECTCOLUMNS (
                'Table 2',
                "Key", 'Table 2'[Key],
                "PIFallsIn", 'Table 2'[PIBasedOnEndDate]
            )
        ),
        [Key],
        [PIFallsIn]
    )
    

    The result is as follow:

     

     

    Best Regards

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