Forum Discussion

cmplieger's avatar
cmplieger
Frequent Visitor
9 years ago
Solved

Looping in PowerQuery

I am trying to run the following Calculation using PowerQuery in PowerBi:

"Service1 Revenue"*"Service1 Rating" + "Service2 Revenue"*"Service2 Rating" + ...

The data comes from these two tables:

╭───╥────────────┬─────────────╮
│ 1 ║   Service  │   Revenue   │
╞═══╬════════════╪═════════════╡
│ A ║ Service 1  │ 10          │
│ B ║ Service 2  │ 100         │
│ C ║ etc        | etc         │
└───╨────────────┴─────────────┘

╭───╥────────────┬─────────────╮
│ 2 ║   Service  │   Rating    │
╞═══╬════════════╪═════════════╡
│ A ║ Service 1  │ 1           │
│ B ║ Service 2  │ 5           │
│ C ║ etc        | etc         │
└───╨────────────┴─────────────┘

I cannot figure out how to write the looping to perform the calculation per service and then sum all the results up.

In excel I guess a vba loop would have worked but not sure how to do that here.

  • cmplieger

     

    Since you already build the relationship between that two tables, you can directly add the product row by row with SUMX() function as BhaveshPatel mentioned.

     

     

    Regards,

4 Replies

    • cmplieger's avatar
      cmplieger
      Frequent Visitor

      I have linked the two tables together and I understand how I could manually calculate it by using filters in dax to select the row I want. But I am dealing with >100k rows so manually selecting each service is not an option.

      • BhaveshPatel's avatar
        BhaveshPatel
        Icon for Super User rankSuper User

        "But I am dealing with >100k rows so manually selecting each service is not an option."

         

        What do you mean by manually selecting each service.

         

        You can use SUMX and RELATED function in a measure to get the aggrgated total of both columns. In DAX, this looping concept is called ROW CONTEXT and Iterator functions are well suited to do the job. It is bit slower but I guess faster than PowerQuery and VBA looping.

         

        TotalRevenue&Ratings:=SUMX(Table1,Table1[Revenue]*RELATED(Table2[Ratings))