Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Summarizing a table to align most recent dates

I've been unable to find/figure out how to summarize a table such that the data is aligned by most recent month, prior month, prior month -1, etc.  My goal is to be able to select the primary source based on qty for each branch based on the most recent 3 months.

 

For example, the following data

Data Table  
Branchsource monthqty
SouthA Vendor Jan-226
SouthA Vendor Feb-225
SouthA Vendor Feb-239
SouthA Vendor Apr-233
SouthB VendorNov-232
SouthB VendorJan-248
NorthB VendorMay-226
NorthB VendorAug-233
NorthC Vendor Dec-228
NorthC Vendor Feb-231
NorthB VendorOct-234
NorthB VendorApr-247
EastA Vendor May-233
EastB VendorApr-246
EastA Vendor Jan-237
EastC Vendor Jun-222
EastA Vendor Jan-227
EastC Vendor Sep-234
EastA Vendor Jul-235
EastC Vendor Feb-248

 

Would Pivot to look like:

If I can create a table from the above that looks like 

I can then select the most recent 3 months, and the result would look like:

Any and all help would be appreciated!  Thanks.

 

  • xifeng_L's avatar
    xifeng_L
    2 years ago

    Anonymous ,

     

    Sorry, I misunderstood your needs before. You can try below measure.

     

    (I can't very well restore your real date due to date formatting issues, so the results may change, but the logic should be correct.)

     

     

    Measure:

    Primary Source = 
    IF(HASONEFILTER('Fact'[Branch]),
        CALCULATE(
            VAR tempTable = SUMMARIZE('Fact','Fact'[source ],"Recent3MonthsTotal",SUM('Fact'[qty]))
            VAR MaxNbr = MAXX(tempTable,[Recent3MonthsTotal])
            RETURN
            MAXX(FILTER(tempTable,[Recent3MonthsTotal]=MaxNbr),'Fact'[source ]),
            TOPN(3,VALUES('Fact'[month]),'Fact'[month])
        )
    )

     

     

    Table Expression:

    Table Name = 
    ADDCOLUMNS (
        ALL ( 'Fact'[Branch] ),
        "Parimay Source",
            CALCULATE(
                CALCULATE(
                    VAR tempTable = SUMMARIZE('Fact','Fact'[source ],"Recent3MonthsTotal",SUM('Fact'[qty]))
                    VAR MaxNbr = MAXX(tempTable,[Recent3MonthsTotal])
                    RETURN
                    MAXX(FILTER(tempTable,[Recent3MonthsTotal]=MaxNbr),'Fact'[source ]),
                    TOPN(3,VALUES('Fact'[month]),'Fact'[month])
                )
            )
    )

     

     

     

     

11 Replies

  • Hi Anonymous ,

     

    You can use below measure to achieve. 

     

     

    Primary Source = 
    IF(HASONEFILTER('Fact'[Branch]),
        VAR tempTable = 
            ADDCOLUMNS(
                VALUES('Fact'[source]),
                "Recent3MonthsTotal",
                    CALCULATE(
                        SUMX(
                            TOPN(3,'Fact','Fact'[new month]),
                            'Fact'[qty]
                        )
                    )
            )
        VAR MaxNbr = MAXX(tempTable,[Recent3MonthsTotal])
        RETURN
        MAXX(FILTER(tempTable,[Recent3MonthsTotal]=MaxNbr),'Fact'[source])
    )

     

    The key step is to convert the months into a sortable format, such as dates.

     

     

    Did I answer your question? If yes, pls mark my post as a solution!

     

    Thank you~

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for taking the time to work on this.  This isn't working exactly as expected.  When I add the following additional rows of data (identified as "West",

      I get the following result:

      you can see that the output is not what you would expect based on the most recent 3 transactions for West.

       

      Also, I need to be able to have this data in a table to use in other relationships and export to other users, the Measure is nice but doesn't do everything I need.  Thanks again for your efforts!

      • xifeng_L's avatar
        xifeng_L
        Super User

        Hi Anonymous ,

         

        May I know what the correct result is for the branch "West"?

         

        Isn't the calculation logic to calculate the qty of the last three transactions by branch and source, and then take the source with the highest total qty?

         

        And, if you want to create a table instead of using a matrix, then you can use the following expression for creating a table:

         

         

        Table Name = 
        ADDCOLUMNS (
            ALL ( 'Fact'[Branch] ),
            "Parimay Source",
                CALCULATE (
                    VAR tempTable =
                        ADDCOLUMNS (
                            VALUES ( 'Fact'[source] ),
                            "Recent3MonthsTotal", 
                                CALCULATE ( 
                                    SUMX ( 
                                        TOPN ( 3, 'Fact', 'Fact'[new month] ), 
                                        'Fact'[qty] 
                                    ) 
                                )
                        )
                    VAR MaxNbr =
                        MAXX ( tempTable, [Recent3MonthsTotal] )
                    RETURN
                        MAXX ( FILTER ( tempTable, [Recent3MonthsTotal] = MaxNbr ), 'Fact'[source] )
                )
        )
        

         

         

         

        Did I answer your question? If yes, pls mark my post as a solution!

         

        Thank you~

         

    • Anonymous's avatar
      Anonymous
      Not applicable

      Your solutions is great xifeng_L 

      Hi, Anonymous 

      Have you solved your problem? If so, can you share your solution here or mark the correct answer as a standard answer to help other members find it faster. Thank you very much for your kind cooperation!

       

       

      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

      Jianpeng Li