Forum Discussion

smko's avatar
smko
Icon for Helper I rankHelper I
5 years ago
Solved

Allocation of header table into detail table

I want to allocate the discount amount happened at the invoice level into line item evenly. Basically the discount will spread evenly into each item, for example InvoiceID 1 has 2 items, hence 20/2 = 10 discount per item

 

Header table

InvoiceIDDiscount
120
240
350

 

Detail table

InvoiceIDLine itemAllocated discount
1A10
1B10
2A13.33
2B13.33
2C13.33
3A25
3B25

I know how to do it in DAX, but I prefer to do the modeling in Power Query. How do I achieve this?

2 Replies

    • smko's avatar
      smko
      Icon for Helper I rankHelper I

      Thanks Fowmy! I'm new to M, could you explain the meaning of (x)=> and the "x" in front of [Discount] and [InvoiceID]?

       

      (x)=> 
          x[Discount]/
          List.Count(
              List.Select( #"Merged Queries"[InvoiceID] , each _  = x[InvoiceID])
          )