2

Pareto

I have created a pareto analysis but the problem is that it's not dynamic because the rankx it's done in a calculated column in customers table in order of the sum of sales in an other table.

Now my #runningtotal is

CALCULATE([M-CY_Sales];FILTER(ALLSELECTED(CUSTOMERS);
    CUSTOMERS[DAX RANK]<=MAX(CUSTOMERS[DAX RANK]));CUSTOMERS[Customer Number] <>BLANK();
    'Detail Sales Report'[Total Actual Revenue - Base]>0)

where I use the calculated column with rankx CUSTOMERS[DAX RANK]. Can I make this measure dynamic? I was thinking to build a table with var and addcoloumn but I'm not able to do it. My actual problem is that I need this pareto dynamic because the filter for district does not function with static column.

I was trying to write something but I don't know how I could create what I want

#RUNNINGTOTAL2 =
var customerranked=ADDCOLUMNS(ALLSELECTED(CUSTOMERS);"ranking";[M-DAX RANK])
return
CALCULATE([M-CY_Sales];FILTER(ALLSELECTED(CUSTOMERS);
    customerranked<=MAX(customerranked));CUSTOMERS[Customer Number]<>BLANK();
    'Detail Sales Report'[Total Actual Revenue - Base]>0)

Obviously this is not correct. I hope you understand my problem. I need to refer a virtual column done with rankx in my measure running total

Sample data edited with measures: [here]: https://mega.nz/#!4t1y0AJI!XF2Vcejm6C50nnssQCS1bJEhnqIGiH1d-mIltVskRgE

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lukeos93
  • 37
  • 7
  • Can you supply sample data for say 5 rows, and just two variables. Do not show us your model because it is too complex to get quick inside. You can generate it easily by creating new table with Enter data option. Even if someone takes time to analyse it, it will not be usable by others. https://stackoverflow.com/help/minimal-reproducible-example You say that you are "not able to do it." But why? What happens when you try something. What error do you get? What exactly have you tried? – Przemyslaw Remin Nov 18 '19 at 15:41
  • I have added a sample data. I need a measure running total without making a column with rankx. How can I do? – Lukeos93 Nov 18 '19 at 16:45
  • Please explain why you don't want RANKX? What do you mean by dynamic measure? Can you create the expected result measure (using dreaded RANKX) in the sample PBIX file? It will be easier to start with. – Przemyslaw Remin Nov 18 '19 at 17:57
  • I have posted a new sample with all the measure, calculated coloumns and visual. I need a way to filter for district and have my ranking that change – Lukeos93 Nov 19 '19 at 00:55

2 Answers2

2

While here is the PBIX file and it may work as you expected, but you should take a broom and sweep your model a little. To get it working just set up the relationship from District to Customer and then to Sales. Or even better, get rid of Districts table. You have that dimension in Customers table. I just slightly changed your measures to get it working but I would change them altogether. Probably you do not need to use FILTER function.

enter image description here

#RUNNINGTOTAL = 
CALCULATE (
    SUM ( 'Sales'[Revenue] ),
    FILTER (
        ALLSELECTED ( Customers ),
        Customers[DAX RANK]
            <= MAX ( Customers[DAX RANK] )
    ),
    'Sales'[Revenue] > 0
)

Anyway I would start it from scratch. Why do you have three tables? What is the purpose of table Districts. You can use the Districts form table Customers to slice Sales.

If you really do not accept corrected invoices and negative sales (ask yourself why), build a measure like that:

[Sales] =
CALCULATE (
    SUM ( FactTable[Sales] ),
    FactTable[Sales] > 0
)

And then refer to it in other measures. Check these posts to see differences of filtering:

DAX Calculate function with and without FILTER

Difference between CALCULATE(m, x=red) vs CALCULATE(m, KEEPFILTERS(x=red))

You may think of building a bridge table, between Customers and Sales, which will contain unique CustomerID of both tables. Dictionaries are updated with lag.

bridge =
DISTINCT (
    UNION (
        DISTINCT (     Sales[CustomerID] ),
        DISTINCT ( Customers[CustomerID] )
    )
)

Give it a shot: https://www.daxformatter.com/

Przemyslaw Remin
  • 6,276
  • 25
  • 113
  • 191
  • The problem is with the measure #runningperc = SUMX ( FILTER ( ALL ( Customers ); Customers[DAX RANK] <= MAX ( Customers[DAX RANK] ) ); 1 / [#countcustomer]) If I select one district it doesn't show the cumulative percentage. How can I do to show it based on the number of customer number visualized? – Lukeos93 Nov 19 '19 at 14:49
  • Have you downloaded my pbix? I edited my answer adding animated gif. For sud region you have what you lurk for. Make sure the region you select has a few customers. Otherwise your cumulative measure for one customer will be 100%. – Przemyslaw Remin Nov 19 '19 at 16:16
  • #runningperc should be a measure that is the cumulative sum of 1/the count of customer. This way it does not function. It's ok only if you have all customers – Lukeos93 Nov 19 '19 at 22:19
  • Anyway I have downloaded your pbix before to do the comment. Have you understood my problem? #runningperc does not function if you select only some districts – Lukeos93 Nov 20 '19 at 09:16
  • Can you please explain what it means "it does not function"? Can you select a specific district and upload two tables (1) one what you get if you select that district (2) the other table, showing what would you like to get (expected results). Please update your question. Space in comments may not comfortable for that. – Przemyslaw Remin Nov 20 '19 at 10:19
  • If you look to #runningperc in the pic of my post you can see that the first number is 0,146% that is 1/COUNTX(ALL(CUSTOMERS);CUSTOMERS[Customer Number]), the second number is 0.146*2 and the 3d is 0.146*3 beacuse #runningperc is SUMX ( FILTER ( ALL ( Customers ); Customers[DAX RANK] <= MAX ( Customers[DAX RANK] ) ); 1 / [#countcustomer]) Now the problem is that if you select only some district this measure doesn't show what I want. Look your gif – Lukeos93 Nov 20 '19 at 11:01
  • Sorry, I can't read your mind. It won't help, if you keep saying "it does not show, what I want." Please read my previous comment. Please review that https://stackoverflow.com/help/minimal-reproducible-example – Przemyslaw Remin Nov 20 '19 at 11:10
  • what I want is what I have said previously in the comment – Lukeos93 Nov 20 '19 at 11:32
1

It is indeed possible, and encouraged to define measures that calculates ranks and cumulative totals on the fly.

However, there are some visualization issues. It looks not possible to use a measure for x axis with "Line and clustered column chart". So it would not be possible to use the Rank measure for x axis. You may put Customer Number to x axis instead, however the chart will look badly with a categorical x axis. It will not fit in the screen and will require a long scroll to reach the right end. Practically, this will hardly work as a pareto chart.

On the basis of this observation, I suggest to use R / Python visual if possible. Here is an example with R visual.

library(dplyr)
library(ggplot2)

totalSales <- sum(dataset$SalesAmount)

dataset <- dataset %>%
    arrange(desc(SalesAmount)) %>%
    mutate(
        CumulativeSales = cumsum(SalesAmount),
        Rank = order(SalesAmount, decreasing = TRUE)
    )

p <- ggplot(dataset, aes(x = Rank, y = SalesAmount)) +
    geom_bar(stat = "identity", width = 1, fill = "#01b8aa")

ymax <- layer_scales(p)$y$range$range[2]

p <- p + geom_line(aes(y = CumulativeSales / totalSales * ymax),
                   size = 1, color = "#fd625e") +
    scale_y_continuous(sec.axis = sec_axis(~ . * totalSales / ymax)) +
    theme_bw()

p

Pareto chart with R

Kosuke Sakai
  • 2,336
  • 2
  • 5
  • 12
  • The graph should function in the original pbix. Are you sure that there is the necessity of phyton? – Lukeos93 Nov 20 '19 at 09:18
  • You state your issue is the rank is not dynamically calculated in the current filter context. When you use the District slicer, the graph will look like missing teeth. Using R or Python is one, maybe not the only, solution to handle that issue. – Kosuke Sakai Nov 20 '19 at 09:38
  • You have done the graph because of the teeth in the visualization caused by the problems in the model. My problem anyway it to have a dynamical model and use rankx as measure in the other measures that now are using the calculated column – Lukeos93 Nov 20 '19 at 09:46
  • 1
    It is definitely possible to calculate ranks and cumulative totals in measure. If visualization is not an issue, you may follow [this](https://exceleratorbi.com.au/pareto-analysis-in-power-bi/). – Kosuke Sakai Nov 20 '19 at 10:05