Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Top 3 Concatenated

Hi All,

 

Thanks in advance. 

Date Table  
DateKeyDateWeek
2019010101/01/20191
xxx
xxx
xxx
2019010808/01/20192

 

Food Table  
DateKeyFoodAmount
20190101Banana500
20190101Orange400
20190101Apple300
20190101Pear200
20190101Grape100
20190108Nuts1000
20190108Cake900
20190108Banana800
20190108Apple700
20190108Pear600

 

 

Desired Output  
WeekFood_Top 3Amount
1Banana,Orange,Apple1500
2Nuts,Cake,Banana4000

Appreciate your efforts. Thanks

  • Anonymous 

    You can try this

    measure = 
    VAR tbl=ADDCOLUMNS('Table',"rank",RANKX(FILTER('Table','Table'[Week]=EARLIER('Table'[Week])),'Table'[Amount],,DESC))
    return CONCATENATEX(FILTER(tbl,[rank]<=3),'Table'[Food],",",[rank],ASC)
    

13 Replies

  • Anonymous , Try this , I have not tested

     

    concatenatex(TOPN(3,ALLSELECTED(Table[Food]),calculate(sum(Table[Amount])),dense), [Food])

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi amitchandak 

      Thanks for the effort. 

       

      The formula seems to be  incomplete, perhaps I'm missing something.

      Just to be clear, the second table is also just three columns, "Week, Amount, Food_Top3".

       

       

  • Anonymous 

    You can try this

    measure = 
    VAR tbl=ADDCOLUMNS('Table',"rank",RANKX(FILTER('Table','Table'[Week]=EARLIER('Table'[Week])),'Table'[Amount],,DESC))
    return CONCATENATEX(FILTER(tbl,[rank]<=3),'Table'[Food],",",[rank],ASC)
    

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi ryan_mayu 

      Much appreciated.

       

      It's returning more than the Top 3. It's returning almost everything instead of just the Top 3.

       

      For more context, my data model

      Date (Date, Datekey, Week No)

      Fact_Table(Datekey, Amount, Food)

    • Anonymous's avatar
      Anonymous
      Not applicable

      Works perfectly. Much appreciated. Have a good day.

  • Anonymous here it is :

     

    Top 3 Food = 
    VAR __table = TOPN ( 3, ALLEXCEPT ('Table','Table'[Week] ), [Sum], DESC )
    RETURN CONCATENATEX ( __table, [Food], "," )

     

     

    Follow us on LinkedIn

     

    Learn about conditional formatting at Microsoft Reactor

    My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

     

    Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      parry2k 

      Thanks.

       

      Still not working. I've updated the original post with the exact data model, perhpas that'll help. 

  • Anonymous it should work, just change the columns:

     

     

     

    Top 3 Food = 
    VAR __table = TOPN ( 3, ALLEXCEPT ('Table','Table'[DateKey] ), [Sum], DESC )
    RETURN CONCATENATEX ( __table, [Food], "," )

    Follow us on LinkedIn

     

    Learn about conditional formatting at Microsoft Reactor

    My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

     

    Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

     

  • Anonymous solution attached, tweak it as you see fit.

     

    Follow us on LinkedIn

     

    Learn about conditional formatting at Microsoft Reactor

    My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

     

    Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

  • v-xiaotang's avatar
    v-xiaotang
    Community Support

    Hi Anonymous 

    Have you solved this question? You can try the solution shared by parry2k. If you need more help, please let me know. If you have solved the question, you can accept the answer helpful as the solution or share you method and accept it as solution, thanks for your contribution to improve Power BI.

     

     

    Best Regards,

    Community Support Team _Tang

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