Forum Discussion

ansa_naz's avatar
ansa_naz
Icon for Continued Contributor rankContinued Contributor
6 years ago
Solved

Count customers making a loss

Hi all

Really hoping someone can help with this measure I am trying to create!

So my data is as follows:

Customer
IDName
AName A
BName B
CName C
Forecasts  
CustIDTypeAmount
AMaterials100
ALabour250
BMaterials1250
BLabour3000
CMaterials10500
CLabour

866

Actuals  
CustIDTypeAmount
AMaterials22
AMaterials335
ALabour54
ALabour111
BMaterials25
BMaterials2100
CLabour979

I need to calculate the count of customers where Actuals.Amount>Forecasts.Amount. So this measure would return 1 using above data, because this is the result if shown in a table:

Customer  
IDNameForecastsActuals
AName A350522
BName B42502125
CName C11366979

Only customer A has Actuals.Amount>Forecasts.Amount. So the measure would therefore return 1.

Any idea on how I can write this, as I am completely lost!

Many thanks for all your help

  • Hi!

     

    I hope this helps answer your question:

     

    Measure = 
    COUNTX(
    FILTER(
    SUMMARIZE(
    'Customer',
    'Customer'[ID],
    'Customer'[Name],
    "Actual", SUM('Actuals'[Amount]),
    "Forecast" , SUM('Forecasts'[Amount])
    ),
    [Actual] > [Forecast]),
    'Customer'[ID]
    )

     

    Regards

  • ansa_naz 

     

    Try this:

    First create Sum measures for both Sales and Forecast:
    Sum of sales = SUM(Sales[sales]) "Actuals" in your example
    Sum of Forecast = SUM(Forecast[forecast])  "Forecasts" in your example

    and then:

     Obvisously use your customer table instead of my "Sales Product" table. (Assuming your Customer table is linked in one-to-many with both your sales table and forecast table)

11 Replies

  • Hi!

     

    I hope this helps answer your question:

     

    Measure = 
    COUNTX(
    FILTER(
    SUMMARIZE(
    'Customer',
    'Customer'[ID],
    'Customer'[Name],
    "Actual", SUM('Actuals'[Amount]),
    "Forecast" , SUM('Forecasts'[Amount])
    ),
    [Actual] > [Forecast]),
    'Customer'[ID]
    )

     

    Regards

    • ansa_naz's avatar
      ansa_naz
      Icon for Continued Contributor rankContinued Contributor

      Thanks JasonTX  This works perfectly! Now just trying to work out how it works, so I can reuse something similar next time! Cheers!

      • JasonTX's avatar
        JasonTX
        Icon for Resolver I rankResolver I

        Not sure if you are familiar with SQL, but it is similair to this query (Asuming everything is in one table):

         

        SELECT COUNT(ID) FROM 

        (SELECT ID , NAME, Sum(ActualAmount), Sum(ForecastAmount)

        FROM TABLE

        GROUP BY ID, NAME) a

  • kentyler's avatar
    kentyler
    Icon for Solution Sage rankSolution Sage

    Interesting problem. 

    I was not able to solve it in one measure.

    I created a measure to calculate whether an individual customer exceeded.

    Customer Exceeds Forecast =
    var TotalActuals = sum(actuals[amount])
    var TotalForecasts = sum(Forecasts[Amount])
    return TotalActuals >= TotalForecasts
    And then I used that measure in a calculated column in the customer table.
     
    Exceeds = if( [Customer Exceeds Forecast],1,0)

    I was then able to count the number of customers that exceed with a measure

    Count of Customers Where Actuals Exceed Forecast = calculate(sum(Customers[Exceeds]),all(customers))
    I had to do this in 2 steps because I could not include the true/false condition of actuals exceeding forecast as a filter in a calculate statement , and I needed to use a calculate statement in order to remove any possible filters placed by a report on the customers table (by using ALL())
     
     
    • ansa_naz's avatar
      ansa_naz
      Icon for Continued Contributor rankContinued Contributor

      Hi kentyler  So this method works, but ideally I was looking to not use calculated columns but do it all in a measure, for ease of usage. Many thanks for the input

  • PaulDBrown's avatar
    PaulDBrown
    Icon for Community Champion rankCommunity Champion

    ansa_naz 

     

    Try this:

    First create Sum measures for both Sales and Forecast:
    Sum of sales = SUM(Sales[sales]) "Actuals" in your example
    Sum of Forecast = SUM(Forecast[forecast])  "Forecasts" in your example

    and then:

     Obvisously use your customer table instead of my "Sales Product" table. (Assuming your Customer table is linked in one-to-many with both your sales table and forecast table)

    • ansa_naz's avatar
      ansa_naz
      Icon for Continued Contributor rankContinued Contributor

      Thanks PaulDBrown that works perfecty too, and is even simpler than JasonTX  solution. Brill! Cant tell about performance, but I will go with this as I can understand it easier. Cheers everyone!

  • jthomson's avatar
    jthomson
    Icon for Solution Sage rankSolution Sage

    If you've got the relationships between tables right, it should just be a case of counting the number of rows of your customer table where the sum of the actual column is greater than the sum of the forecast column?

    • ansa_naz's avatar
      ansa_naz
      Icon for Continued Contributor rankContinued Contributor

      jthomson  Yes thats correct. However, the total Forecast and Actual amounts dont exist in the Customers table

      • kentyler's avatar
        kentyler
        Icon for Solution Sage rankSolution Sage

        Those totals are calculated inside the measure

        var TotalActuals = sum(actuals[amount])
        var TotalForecasts = sum(Forecasts[Amount])
        I
        If you'd like to do a zoom screen share on this I'm around tomorrow and we can go over it.