Search code examples
powerbidax

Count all occurrences of reference, ignoring visual and dividing equally


I've come across another fuzzy scenario and I'm wondering if anyone may be able to help.

I have three tables

Invoices

Invoice Ref Value
Inv1 10,000
Inv2 9,999

Invoice Categories

Invoice Ref Category
Inv1 1
Inv2 2
Inv2 2

Categories

Category Ref Description
1 Stuff
2 Things

They join to create a situation where an invoice can have multiple categories.

I have a PowerBI visual that displays like this

Invoice Ref Count Value Category
Inv1 1 10,000 Stuff
Inv1 1 10,000 Things
Inv2 1 9,999 Things

However, I need it to be (counting the total rows of the reference and dividing equally between the two)

Invoice Ref Count Value Category
Inv1 2 5,000 Stuff
Inv1 2 5,000 Things
Inv2 1 9,999 Things

Sorry for the table spam. I can't find a decent way to articulate it. Also, what is this operation called so I can do some reading?


Solution

  • This feels wrong or dirty - anyways try the following Measures:

    Category count = 
      var meCount = COUNTROWS('Invoice Categories')
      var allCount = 
        CALCULATE(
          COUNTROWS('Invoice Categories'),
          ALLSELECTED('Categories')
        )
      return IF(NOT ISBLANK(meCount), allCount)
    
    
    Invoice Category amount = 
      var s = SUM(Invoices[Value])
      return
        IF(
          ISINSCOPE('Categories'[Description]),
          DIVIDE(s, [Category count]),
          s
        )
    

    enter image description here