Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Check if Salespeople Split Accounts in Given Month

Hi all, 

 

I have a problem where I'm trying to replicate the COUNTIFS function of Excel in PowerBI. Basically, I have sales data that gives me the Month of sales, Account Name and Salesperson. For calculating commission, the Salesperson gets full commission if only they sold to that customer in that month. If two salespersons sell to the same customer in the same month, the pot is split 50/50. If Person A sells to Customer A in Month 1 and Person B sells to Customer A in Month 2, no commissions are split. 

 

This is very easy to do in Excel but I'm finding it tricky in PowerBI - see attached Raw Data and Expected Outcome. I don't think I can use CALCULATE(FILTER(...)) as I need the values to appear in the column so that I can then filter for all the '0.5s'. 

 

Any advice on how this could be achieved in BI? Your help is greatly appreciated! OutcomeOutcomeDataData

2 ACCEPTED SOLUTIONS
Icey
Community Support
Community Support

Hi @Anonymous ,

 

@Greg_Deckler’s formula is great, but there are two typos. I have marked it in the screenshot below. After modifying it, the result is the same as you want.

including.jpg

 

 

Best Regards,

Icey

 

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

View solution in original post

Icey
Community Support
Community Support

Hi @Anonymous ,

 

To my understand, the first value should be 1, right?

Updated.PNG

 

If so, try this:

 

1. Add an index column in Power Query Editor.

 

2. Create a column like so:

Column =
VAR __Account = [Account]
VAR __Month =
    MONTH ( [Month] )
VAR __Year =
    YEAR ( [Month] )
VAR __Index = [Index]
VAR __Salesperson = [Salesperson]
VAR __Table =
    ADDCOLUMNS (
        'Table (2)',
        "__Year", YEAR ( [Month] ),
        "__Month", MONTH ( [Month] )
    )
VAR __Table1 =
    SUMMARIZE ( __Table, [__Year], [__Month], [Account], [Salesperson], [Index] )
VAR result1 =
    COUNTROWS (
        DISTINCT (
            SELECTCOLUMNS (
                FILTER (
                    __Table1,
                    [__Month] = __Month
                        && [__Year] = __Year
                        && [Account] = __Account
                ),
                "__Salesperson", [Salesperson]
            )
        )
    )
VAR result2 =
    COUNTROWS (
        FILTER (
            __Table1,
            [__Month] = __Month
                && [__Year] = __Year
                && [Account] = __Account
                && [Salesperson] = __Salesperson
                && [Index] <= __Index
        )
    )
RETURN
    IF ( result2 > 1, 0, IF ( result1 = 1, 1, 0.5 ) )

includ.JPG

 

 

Best Regards,

Icey

 

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

View solution in original post

8 REPLIES 8
Icey
Community Support
Community Support

Hi @Anonymous ,

 

@Greg_Deckler’s formula is great, but there are two typos. I have marked it in the screenshot below. After modifying it, the result is the same as you want.

including.jpg

 

 

Best Regards,

Icey

 

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

Anonymous
Not applicable

@Icey @Greg_Deckler 

 

Thanks again for your help with this. I wondered if you could help me with this slight modification. 

 

How would I change the formula if I wanted to only count the account once and there were multiple entries in a given month? See attached image - in this, John sold to A twice in the same month, so I only want the first entry to register as a 1 for that month. When he sells to them again in Month 2, he gets a 1 as he only sold to that company once. 

 

I've tried adding in helper columns and calculating from that but it isn't working. I'm sure it must be a slight tweak to your formulas but I can't find that tweak at the minute. 

 

Thank you again!! Updated.PNG

Icey
Community Support
Community Support

Hi @Anonymous ,

 

To my understand, the first value should be 1, right?

Updated.PNG

 

If so, try this:

 

1. Add an index column in Power Query Editor.

 

2. Create a column like so:

Column =
VAR __Account = [Account]
VAR __Month =
    MONTH ( [Month] )
VAR __Year =
    YEAR ( [Month] )
VAR __Index = [Index]
VAR __Salesperson = [Salesperson]
VAR __Table =
    ADDCOLUMNS (
        'Table (2)',
        "__Year", YEAR ( [Month] ),
        "__Month", MONTH ( [Month] )
    )
VAR __Table1 =
    SUMMARIZE ( __Table, [__Year], [__Month], [Account], [Salesperson], [Index] )
VAR result1 =
    COUNTROWS (
        DISTINCT (
            SELECTCOLUMNS (
                FILTER (
                    __Table1,
                    [__Month] = __Month
                        && [__Year] = __Year
                        && [Account] = __Account
                ),
                "__Salesperson", [Salesperson]
            )
        )
    )
VAR result2 =
    COUNTROWS (
        FILTER (
            __Table1,
            [__Month] = __Month
                && [__Year] = __Year
                && [Account] = __Account
                && [Salesperson] = __Salesperson
                && [Index] <= __Index
        )
    )
RETURN
    IF ( result2 > 1, 0, IF ( result1 = 1, 1, 0.5 ) )

includ.JPG

 

 

Best Regards,

Icey

 

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

Anonymous
Not applicable

@Icey  This is amazing - you're a lifesaver, thank you! 

Anonymous
Not applicable

This is fantastic, thank you both!! 

Greg_Deckler
Super User
Super User

@Anonymous Perhaps try this:

Included? Column =
  VAR __Account = [Account])
  VAR __Month = MONTH([Month])
  VAR __Year = YEAR([Month])
  VAR __Table = ADDCOLUMNS('Table',"__Year",YEAR([Month]),"__Month",MONTH([Month]))
  VAR __Table1 = SUMMARIZE(__Table,[Year],[Month],[Account])
RETURN
  IF(COUNTROWS(FILTER(__Table1,[__Month]=__Month && [__Year]=__Year && [Account]=__Account))=1,1,.5)

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

@Greg_Deckler  - Thank you for this! However, it doesn't seem to consider the Salesperson? When I run this formula on my dataset, it seems to count how many times the name appears, but doesn't consider whether 2 persons have sold to 1 company in 1 month? 

 

Apologies if I've missed something, I'm very new to this. 

@Anonymous Nope, I missed that, should be as below. Also updated this so if the same person sells twice in the same month to the same account that it returns 1 and not .5.

Included? Column =
  VAR __Account = [Account])
  VAR __Month = MONTH([Month])
  VAR __Year = YEAR([Month])
  VAR __Table = ADDCOLUMNS('Table',"__Year",YEAR([Month]),"__Month",MONTH([Month]))
  VAR __Table1 = SUMMARIZE(__Table,[Year],[Month],[Account],[Salesperson])
RETURN
  IF(COUNTROWS(DISTINCT(SELECTCOLUMNS(FILTER(__Table1,[__Month]=__Month && [__Year]=__Year && [Account]=__Account),"__Salesperson",[Salesperson])))=1,1,.5)

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.