How can I refer to column created in var dynamically? - powerbi

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

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.
#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/

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

Related

How to Calculate values for inactive relationship with top N and calculate without Unpivoting

I have two data bases.One contain carder and other one contain data.What I want is once I select the designation from the slicer.Then select the the employee and need to show higher defect rate styles.I have set little slicer to select higher 1 ,2 ,3 defect rate styles. .My measures as below.
Total Check = SUM(Records[Check Qty])
Total Defects = SUM(Records[Defects])
Selected_Top_N = SELECTEDVALUE('Top N'[Column1])
Defect pct = CALCULATE((DIVIDE([Total Defects],[Total Check])),TOPN([Selected_Top_N],ALL(Records[Style]),DIVIDE([Total Defects],[Total Check]),DESC),VALUES(Records[Style]))
This "Defect pct" measure works fine for Executive grade. Because it has active relationship. For qc it shows blank. My question is how to modify "Defect pct" measure using "userrelationship" or any other dax function to see top styles once I clicked qc from designation slicer and qc from the employee list.Without unpivoting I can get check qty and defect qty like below with %.by selected value for designation.'code'
Selected_designation = SELECTEDVALUE(Carder[Desingation])
SWITCH(true(),[selected_designation] ="Executive",DIVIDE([Total Defects],[Total Check]),
[selected_designation] ="QC",CALCULATE(DIVIDE([Total Defects],[Total Check]),USERELATIONSHIP(Records[QC],Carder[EMPLOYEE])))
'then using switch function I get these with two calulations.one for the direct relation ship.and other calculation with userrelationship for inactive one.I want rank this % with topN.End result must be once I click excetive I want rank higher defect % pct according to the selected executive and once I click qc I want the same.
You need first to convert your fact table into this shape using unpivoting the last 2 columns.
Like This:
Then Create a designation Table consisting of 2 columns:
Like This:
Your Final Model View should appear like this:
(Updated) DAX Code For Defect pct:
Defect pct =
VAR selection =
SELECTEDVALUE ( Carder[Desingation] )
VAR conditional_selection =
SWITCH (
selection,
"Executive", CALCULATE ( MAXX ( Records, DIVIDE ( [Total Defects], [Total Check] ) ) ),
"QC",
CALCULATE (
MAXX ( Records, DIVIDE ( [Total Defects], [Total Check] ) ),
USERELATIONSHIP ( Records[QC], Carder[EMPLOYEE] )
)
)
RETURN
conditional_selection

Power BI DAX measure calculation

Step1:
I have created a calculated table containing Level, Location, L2code from level sales data. I need to create a report that will count rows based on the level1 group.
Note that there are more levels in the table. This is only an example of one of the levels.
How can I count the rows for each level1?
Step2:
I need to create all combinations of counts based on location and l2 code and count the numbers.
like in example 2 location and 8 distinct l2code so it should be 2*8 =16 total possible rows.
How can I achieve this using the DAX measure?
Source data file
Output report
first one is a simple measure as below-
DistinctRowCount = Count(your_table_name[Level1])
Second one is bit tricky and you can try this below measure-
CrossCount =
var distinct_location =
calculate(
distinctcount(your_table_name[Location]),
allexcept(
your_table_name,
your_table_name[Level1],
your_table_name[Location]
)
)
var distinct_l2code =
calculate(
distinctcount(your_table_name[l2code]),
allexcept(
your_table_name,
your_table_name[Level1],
your_table_name[Location],
your_table_name[l2code]
)
)
return distinct_location * distinct_l2code
Sample output-

How to calculate dynamic Required Measure using TotalMeasure in DAX in which i need to exclude the BLANK() & 0 in NetRevenue for those SKU's?

I want to calculate the RequiredMeasure column in DAX as shown in the below figure.
If I remove the Product SKU then the value should not change it should show the total values of RequiredMeasure as 200 only as shown in the below figure.
Please find the below screenshot for clear understanding.
SUMX is the function you need, to iterate over all rows of the table, and calculate an expression:
Required Measure =
SUMX (
Table1,
Table1[Net Revenue] * Table1[Total Measure]
)
Worked example file: https://pwrbi.com/so_55255241/

DAX - Histogram for relative frequencies and total column

I built a diagram that displays the relative frequencies of my clusters (on the values in the column) and the cumulated frequencies (on the values on the line).
My chart has this aspect:
I would like to add a new column to the right that is equal to the sum of all the values of the previous columns.
The relative frequencies are created using the code below:
"Frequencies UL" :=
CALCULATE (
DISTINCTCOUNT ( 'table1'[Column1] );
USERELATIONSHIP ( 'Order Cluster'[Cluster Name]; table1[tag Cluster] );
SUMMARIZE ( table1; table1[tag Cluster] )
)
I would really appreciate some help!
Thanks
Simply it was necessary to do this:
"Frequencies UL" := IF(SELECTEDVALUE('Order Cluster'[Is Total]);
CALCULATE(DISTINCTCOUNT ('table1'[Column1]); ALL('Order Cluster')); DISTINCTCOUNT('table1'[Column1]))
And this is the result I got!
I'd suggest creating a new table to use for your x-axis.
If your 'Order Cluster' table looks like this:
ClusterName Order
ClusterA 1
ClusterB 2
... ...
ClusterZ 26
You want to add a Total row to the end so try something along these lines:
NewTable = UNION('Order Cluster', {("Total", MAX('Order Cluster'[Order]) + 1)})
Use NewTable[ClusterName] for your chart's x-axis and tweak your cumulative measure to reference NewTable[Order] in the FILTER inequality.
You'll also need to adjust your frequency measure to handle the case when you have the Total cluster (use an IF or SWITCH) and make sure you're evaluating within the correct filter context. Something like this logic:
IF( MAX( NewTable[ClusterName] ) = "Total",
CALCULATE( [Frequency Calc], ALLSELECTED( table1 ) ),
CALCULATE( [Frequency Calc],
FILTER(
ALLSELECTED( table1 ),
table1[tag Cluster] = MAX( NewTable[Order] )
)
)
)
P.S. You might be better off adding the total row to your 'Order Cluster' table in the query editor instead of having another table floating around. In any case, the logic is similar; add a total row to the column you're using for your axis and adjust your measures to handle that category how you want.
A waterfall chart might be another option to consider (and doesn't need nearly as much work), though I don't know that you can include the percent info except in the tooltip.

PowerBI - Aggregate multiple rows using combination of fixed and variable calculations mapped to ID in separate table

I have a rather complicated data set, but will attempt to simplify it for this post.
I have contract data in table F1_Contract with a simplified format of:
I am attempting to calculate the expected profit from contracts.
The first thing that I needed is to calculate the incremental Volume from each contract that was valid between the Current Date and the Next Date, depending upon the date slicer used in the view. After much pain and anguish, someone pointed me to a post on StackOverflow that resolved my issue:
Create a Disconnected Date table, use that as the Date Slicer, then calculate the date difference between the Current Date, START_DATE of the slicer, Next Date, and END_DATE of the slicer.
The resulting Measure is
DELTA DATE =
CALCULATE (
SUMX (
F1_Contract,
DATEDIFF (
MAX ( MAX ( F1_Contract[CURRENT_CONTRACT_DATE] ), [Disconnected_MIN_Date] ),
MIN ( MAX ( F1_Contract[NEXT_CONTRACT_DATE] ), [Disconnected_MAX_Date] ),
DAY
)
),
FILTER (
F1_Contract,
F1_Contract[CURRENT_CONTRACT_DATE] <= [Disconnected_MAX_Date]
&& F1_Contract[NEXT_CONTRACT_DATE] >= [Disconnected_MIN_Date]
)
)
I then take that Measure and multiply it by the VOLUME_PER_DAY to get the incremental Volume for the view with the following formula:
Incremental Cumulative VOLUME =
CALCULATE(SUMX(F1_Contract,F1_Contract[VOLUME_PER_DAY]* [DELTA DATE]))
To calculate F1 Revenue and F1 Cost, I take the F1 Unit Cost and the appropriate F1 price based on the Incremental Volume and derive the following measures:
Incremental F1 Revenue =
CALCULATE (
MAX (
SUMX (
F1_Contract,
[Incremental Cumulative VOLUME] * [F1 Sell Rate # GAD Per Shipment]
),
[Calc F1 MinCharge]
)
)
Incremental F1 Cost =
CALCULATE (
SUMX ( F1_Contract, [Incremental Cumulative VOLUME] * F1_Contract[F1_Cost] )
)
This all works great! I can create a report at the ID level, Indicator level, or the Lane level and all of the numbers are correct.
The problem is that I have a second revenue table, F2_Contract_Revenue, that consists of F2 revenues formatted like the following (note there may be 0 to 15 rows in F2_Contract_Revenue for any given ID in F1_Contract)
F2_Contract_Revenue:
Although the ID in F1_Contract is unique, just to be on the safe side I have a separate DISTINCT_ID table that I have used to link the ID from F1_Contract and F2_Contract_Revenue.
Now I need to calculate the F2 revenue for each ID; using a visual formula of:
If(BASIS = “FLAT”, F2_Unit_Rev, MAX(F2_Min, (Incremental Volume * F2_Unit_Rev))
The Measure I created after about 30 attempts is:
F2 Revenue =
CALCULATE (
(
SUMX (
F2_Contract_Revenue,
(
MAX (
[Incremental Cumulative VOLUME]
* IF ( F2_Contract_Revenue[BASIS] = "RATE",
F2_Contract_Revenue[F2_Unit_Rev], 0 ),
F2_Contract_Revenue[F2_Min]
)
)
+ IF ( F2_Contract_Revenue[BASIS] = "FLAT",
F2_Contract_Revenue[F2_Unit_Rev], 0 )
)
),
FILTER (
F2_Contract_Revenue,
F2_Contract_Revenue[ID] = RELATED ( F1_Contract[ID] )
)
)
This works correctly at the Lane level. However, in the views at the ID level, it is slightly off (I have not been able to track down why) and at the Indicator level is it exponentially off.
I need to use this in a formula that will be represented as
F1 Revenue + F2 Revenue – F1 Cost which is of course also exponentially off at the INDICATOR level (note there are multiple rows of INDICATOR = 1 and a single row of INDICATOR = 2).
The data is proprietary, so I cannot share the PowerBI file, however, I can answer more specific questions with the data that I’ve cleaned up here.
Any advice, thoughts, corrections, help is greatly anticipated and appreciated!!!
[Suggest you show the moedel with Relation direction]
At a quick view of this, I think the problem is the data model - relation direction.
you work fine on [Incremental F1 Revenue] (ID level, Indicator level, or the Lane level).
these are all in 'F1 table'.
But, when it comes to [F2 Revenue], you get the Problem.
(measure is involve 'F1 table' & 'F2 table').
FILTER (
F2_Contract_Revenue,
F2_Contract_Revenue[ID] = RELATED ( F1_Contract[ID] )
Also you said that you
just to be on the safe side I have a separate DISTINCT_ID table
so I like point out is, may you show your model (dimTable - factTable) for problem shooting.
In Dax, its all about Relation and Model XD
(after you know about dax so much, model might be the really problem.)
after reading a lot more posts and going through helpful videos, I determined the problem was in the ordering of the values in my aggregation statement as well as insufficient bounding of the time dimension.
I have three steps
a) for each row of the F2 table, determine if the value is a FLAT or RATE; if FLAT, use that value, if RATE, then multiply that value by the dynamic measure that is determining volume based on the report Date Slicer
b) compare the FLAT or RATE outcome to a MIN value in the table
c) aggregate those values using the link between the F1 and F2 tables.
The calculation that works is:
F2 =
CALCULATE
(SUMX(F2_Contract_Revenue,
((MAX(IF(F2_Contract_Revenue[BASIS]="RATE",F2_Contract_Revenue[F2_Unit_Rev]* [Incremental Cumulative Volume],F2_Contract_Revenue[F2_Unit_Rev]),F2_Contract_Revenue[F2_MIN]))) )
,FILTER(F2_Contract_Revenue,F2_Contract_Revenue[ID]=RELATED(F1_Contract[ID]))
,FILTER ( F1_Contract,
F1_Contract[CURRENT_CONTRACT_DATE] <= [Disconnected_MAX_Date]
&& F1_Contract[NEXT_CONTRACT_DATE] >= [Disconnected_MIN_Date] ))
I appreciate the response and for the next issue I will create a generic modle that I can post as an example.