Is there a way to put calculated column in ALL()? - powerbi

I'm trying to remove filters from a calculated column with ALL(), and the measure seems to ignore it
"Class" is a calculated column.
I want to add measure always showing 100 (the grand total)
There are outer filters on the table so I don't want to use ALL(TABLE)
However, CALCULATE(SUM([Total Sales]), ALL(Product, Class)) doesn't work.

You can use the ALL() function on a list of columns like this:
CALCULATE ( SUM ( Table1[Sales] ), ALL ( Table1[Product], Table1[Class] ) )
This should work fine even if Class is a calculated column.

Related

calculated column stops working when exdternal filter used

I have some Fact Revenue, I am trying to group by Conca, and display the values only if negative…
For doing it I have this calculated column:
=
VAR name1 = Revenue[Conca]
VAR name2=
CALCULATE (
SUM ( Revenue[CostOfQuality] ),
FILTER ( Revenue, Revenue[Conca] = name1 )
)
RETURN
if (name2>0, 0, Revenue[CostOfQuality])
It works:
(highest value is 0 as expected):
Now...
If I drag fiscal year it stops working:
Why is it that I see numbers higher than 0?? (I want it to still work even if I bring other filters...)
a calculated column is computed computed once on the whole dataset after the data are loaded, and is basically a "static column".
Whatever the expected result, you are looking for a measure, which gets computed in the current context
Calculated columns and calculated tables are different from measures, since they are static: You can filter them, but they don't recalculate on filter changes.

CONCATENATEX in multi-row card (DAX)

I have the sample data below
I used the code below to get the result as below for measure.
CONCATENATEX(VALUES(Table[Type]),Table[Type],", ")
It worked for the table view. However, when I do the count in multi-row card, it won't work.
My desired outcome is something looks like below. Looking to have the view in Multi-row card to get the outcome like that. Please help!!! Either measure or column will be fine as long as it gives me the result.
The way you described it, you need a calculated column first
Column = CALCULATE(CONCATENATEX(VALUES('Table'[type]),'Table'[type],","), ALLEXCEPT('Table','Table'[id]))
Then you can create a measure to show the count
Measure =
COUNTX ( SUMMARIZE ( 'Table', 'Table'[id], 'Table'[Column] ), [Column] )

DAX DEFINE returns column not found error

This works:
EVALUATE
ADDCOLUMNS(
FILTER (Sales, [StoreKey] = 155 ) ,
"Margin", (Sales[SalesAmount] - Sales[TotalCost])
)
However, if I try to define a function, I get an error:
DEFINE VAR Margin = Sales[SalesAmount] - Sales[TotalCost]
EVALUATE
ADDCOLUMNS(
FILTER (Sales, [StoreKey] = 155) ,
"Margin", Margin
)
Query (1, 21) A single value for column 'SalesAmount' in table 'Sales'
cannot be determined. This can happen when a measure formula refers to
a column that contains many values without specifying an aggregation
such as min, max, count, or sum to get a single result.
What is this error and how to fix it?
There are 2 problems with the second code:
It's missing Row Context
VAR is not a function, it's a variable. Functions in DAX are Measures
What is "Row Context"? In short, in Power BI data is stored in a database. When you are referring to a column in the database, you must either: aggregate the data in it (i.e., sum it), or provide a specific row ("row context").
In your first code, function ADDCOLUMNS is an iterator. It means that it loops a table row by row, and for each record does a calculation. Since during each iteration it "knows" which row it's on, you can refer to the table fields without problems.
In the second code, this line:
Margin = Sales[SalesAmount] - Sales[TotalCost]
has no row context - it does not know which record to use for the calculation, and hence the error. You must either aggregate the data first, or put this calculation inside an iterator.
In this particular case, the simplest solution is to use aggregation:
DEFINE
MEASURE 'Sales'Margin = SUM ( Sales[SalesAmount] ) - SUM ( Sales[TotalCost] )
EVALUATE
ADDCOLUMNS (
CALCULATETABLE ( Sales, Sales[StoreKey] = 155 ),
"Margin", [Margin]
)
Here, we first aggregate amounts and costs and calculate margin. Then, inside of ADDCOLUMNS we iterate table Sales filtered for a specific store, and for each row we call the measure defined above.
If you need to use an iterator instead of aggregation, you can do something like this:
DEFINE
MEASURE 'Sales'Margin = SUMX(Sales, Sales[SalesAmount] - Sales[TotalCost] )
EVALUATE
ADDCOLUMNS (
CALCULATETABLE ( Sales, Sales[StoreKey] = 155 ),
"Margin", [Margin]
)

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.

DAX Measure to determine % of parent row in column chart

I've been struggling to find a way to achieve the result of a matrix visualization in Power BI, in a line and clustered column chart.
I have tried using ALLSELECTED, ALL, ALLEXCEPT in order to ignore one filter but no matter how I try it, I'm not reaching my end goal.
CarCountPerCategory =
CALCULATE(
DISTINCTCOUNT(Table[CarID]),
ALLSELECTED(Table[Filter])
)
This still breaks down the measure to the Filter level instead of ignoring it. I am looking for something that will give me the total distinct count regardless of the Filter column. E.g. TimesReshopped 1 - 190 on all 3 columns, TimesReshopped 2 - 182 on all 3 columns of the Filter Column series. I need this as a sidestep in order to divide the distinct car count to the total distinct car count per times reshopped in order to reach the percentage that I am looking for. (the one in the matrix)
Can someone help me with some advice? Thank you.
Try this:
[Total Category Count]:=
SUMX ( SUMMARIZE ( Table, Table[TimesReshopped] ), [Count of CarID] )
I got to the bottom of this by using ALLEXCEPT and providing all the filter that I was using:
CarCountPerCategory =
CALCULATE(
DISTINCTCOUNT(Table[CarID]),
ALLEXCEPT(Table,Table[Year],Table[Month],Table[LocationName],Table[CustCode],Table[TimeReshoped])
)