Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Getting the previous record values

StoreNameFormDateSum of TotalScore
A09-11-2022 00:002
A05-05-2023 00:0095
A16-11-2023 00:0099
A23-04-2024 00:0098
A19-11-2024 00:0096
B02-11-2022 00:002
B12-04-2023 00:0094
B08-11-2023 00:0095
B09-04-2024 00:0090
B13-11-2024 00:0094
C15-11-2022 00:001
C12-04-2023 00:0092
C15-11-2023 00:0099
C17-04-2024 00:0089
C21-11-2024 00:0092
D15-11-2022 00:001
D12-04-2023 00:0094
D15-11-2023 00:0098
D17-04-2024 00:0096
D21-11-2024 00:0098

 

I have the in the above format and need to display the data in the table visual like the below format.

Store Name      Latest Score  Previous Score

A                            96                             98

B                            94                             90

C                            92                             89

D                            98                            96

 

Any ideas how to achive this in Power BI. Any inputs must be approciated. Thanks for advance.

  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi Anonymous ,

    I create a table as you mentioned.

    Then I think you can create a new table and here is the DAX code.

    NewTable = 
    SUMMARIZE (
        'Table',
        'Table'[StoreName],
        "Latest Score",
            CALCULATE (
                MAX ( 'Table'[Sum of TotalScore] ),
                'Table'[FormDate] = MAX ( 'Table'[FormDate] )
            ),
        "Previous Score",
            CALCULATE (
                MAX ( 'Table'[Sum of TotalScore] ),
                'Table'[FormDate]
                    = MAXX (
                        FILTER (
                            'Table',
                            'Table'[StoreName] = EARLIER ( 'Table'[StoreName] )
                                && 'Table'[FormDate] < MAX ( 'Table'[FormDate] )
                        ),
                        'Table'[FormDate]
                    )
            )
    )

     

     

    Best Regards

    Yilong Zhou

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

5 Replies

  • You could use the RANKX function (descending order) where 1 = your latest date and 2 = your next latest date.  You could use the TOPN function to retrieve the top two values each category (i.e. latest date and second latest date) and use the MINX function to get the lowest of these two values.  You could use the INDEX function to retrieve the 2nd value from your list when ordered by descending date values.

     

    There's lots of options with DAX 🙂

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

    I create a table as you mentioned.

    Then I think you can create a new table and here is the DAX code.

    NewTable = 
    SUMMARIZE (
        'Table',
        'Table'[StoreName],
        "Latest Score",
            CALCULATE (
                MAX ( 'Table'[Sum of TotalScore] ),
                'Table'[FormDate] = MAX ( 'Table'[FormDate] )
            ),
        "Previous Score",
            CALCULATE (
                MAX ( 'Table'[Sum of TotalScore] ),
                'Table'[FormDate]
                    = MAXX (
                        FILTER (
                            'Table',
                            'Table'[StoreName] = EARLIER ( 'Table'[StoreName] )
                                && 'Table'[FormDate] < MAX ( 'Table'[FormDate] )
                        ),
                        'Table'[FormDate]
                    )
            )
    )

     

     

    Best Regards

    Yilong Zhou

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      I have joined the new table with existing table, now it's filteing the previous values aswel.  it's worked fine.. thanks for your help. 

       

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for your help, my requiremet got littlebit complicated.. these values will change based the year filter selected by the ender user in the slicer.