DAX Power BI conditional formatting bar chart - powerbi

I have a bar chart, which colors the 3 worst performing Categories red.
This is the measure I wrote:
color 3 worst =
VAR color =
IF
(SELECTEDVALUE(
superstore[Sub-Category])
IN TOPN (
3,
FILTER(ALLSELECTED(superstore[Sub-Category]),CALCULATE(SUM(superstore[Sales]))>0),
CALCULATE(SUM(superstore[Sales])),
ASC),
"#FD625E",
"#01B8AA"
)
RETURN
color
However, when I use filter for a region and they sell less then 4 items, i do not want to color them red. Here is an example:
i tried this but failed:
color 3 worst =
VAR color =
IF
(SELECTEDVALUE(
superstore[Sub-Category])
IN TOPN (
3,
FILTER(ALLSELECTED(superstore[Sub-Category]),CALCULATE(SUM(superstore[Sales]))>0),
CALCULATE(SUM(superstore[Sales])),
ASC),
"#FD625E",
"#01B8AA"
)
VAR less4 =
IF(
DISTINCTCOUNTNOBLANK(superstore[Sub-Category])>3,
color,
"#01B8AA"
)
RETURN
less4

Related

Slicer with few TOP N options // COUNT

I'm having problems with implementing TOP N slicer into my dashboard. I would like to have a single select TOP 5, TOP 10, TOP 20 and TOP 30 slicer which will show items with the biggest count.
I partially managed to achieve this and it works while I have only one column added to the y-axis (I use horizontal bar chart) and my measure to the x-axis but when I add another column to legend field everything falls apart.
These are my measures:
TopN_test =
VAR Selected_Top = SELECTEDVALUE('Top N'[Select Top N])
RETURN
SWITCH(TRUE(),
Selected_Top = 0,[count_all_current],
RANKX(
ALLSELECTED(Skills[Skill correct]),
[count_all_current]
)
<= Selected_Top,
[count_all_current]
)
count_all_current =
CALCULATE(
COUNT('Skills'[Skill correct]),
FILTER('Skills', 'Skills'[DateIndex] = 6),
FILTER('Skills', 'Skills'[Proficiency]<>"-")
)
I will be grateful for any advice how to adjust it.
I added a calculate and Selected_Top Variable as a filter. Can you please check if this code works for you.
TopN_test =
SWITCH (
TRUE (),
Selected_Top = 0, [count_all_current],
RANKX ( ALLSELECTED ( Skills[Skill correct] ), [count_all_current] ) <= Selected_Top, CALCULATE ( [count_all_current], Selected_Top )
)

DAX How to return a table based on a condition - workaround for IF to return a table

How to return DAX table based on a condition? The IF function cannot return a table in DAX.
IF( 1=1, table_1, table_2 )
It raises an error: The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.
I would like to use a slicer to choose between multiple tables, which table later can be used for the alternating dynamic filter context in CALCULATE.
CALCULATE( [Measure],
IF( Condition,
table_1,
table_2
)
)
To make the problem more challenging I would like the table_1 and table_2 to have different set of columns. So combination of UNION and FILTER function won't do.
UNION(
FILTER( table_1, condition ),
FILTER( table_2, NOT condition)
)
As a possible approach, we might pick up a measure based on a slicer choice:
IF( Slicer_Selection_Condition,
[M1], // Measure with filter set 1
[M2] // Measure with filter set 2
)
But I do not want to go this way because it multiplies the number of required measures for each slicer combination.
If we could surpass the IF limitation, we could very usefully apply it. Imagine, we have a slicer to choose a measure among [Quantity], [Value], [Cost]. And we also have another slicer to choose the filter set. We could handle it with a one measure:
CALCULATE(
SWITCH( Measure_Slicer, 1, [Quantity], 2, [Value], [Cost] ), // measure choice
SWITCH( Filter_Slicer, 1, table_1, 2, table_2, table_3 ) // filter choice
)
Here is a table to recreate problem:
Table =
DATATABLE (
"Color", STRING,
"Shape", STRING,
"Quantity", INTEGER,
"Value", INTEGER,
{
{ "Red" , "Circle" , 1, 10 },
{ "Red" , "Triangle", 1, 10 },
{ "Blue" , "Circle" , 1, 10 },
{ "Blue" , "Triangle", 1, 10 },
{ "Yellow", "Square" , 1, 10 }
}
)
And measures:
M_Quantity = SUM( 'Table'[Quantity] )
M_Value = SUM( 'Table'[Value] )
Desired Measure =
VAR Measure_Slicer = 1 // Either 1 OR 2
VAR Filter_Slicer = 1 // Either 1 OR 2
VAR table_1 = SUMMARIZE( 'Table', 'Table'[Color] )
VAR table_2 = SUMMARIZE( 'Table', 'Table'[Color], 'Table'[Shape] )
RETURN
CALCULATE(
SWITCH( Measure_Slicer, 1, [M_Quantity], [M_Value]), // Measure choice
SWITCH( Filter_Slicer, 1, table_1 , table_2 ) // Filter choice
)
Here, a solution for one slicer which lets the user choose between different measures. How to select different data tables, is something I am also curious about :)
Based on your table Table above and the two measures M_Quantity and M_Value, the only things you need to add are:
A table which includes the possibilities for the selection:
choice_measures =
DATATABLE (
"Id", INTEGER,
"Use", STRING,
{
{ 1, "Quantity"},
{ 2, "Value" }
}
)
One more measure:
M_Measure =
VAR MySelection = SELECTEDVALUE(choice_measures[Id], "Show all") RETURN
SWITCH(
TRUE(),
MySelection = 1, [M_Quantity],
MySelection = 2, [M_Value],
""
)
A slicer for choice_measures[Id] where the user can choose between the two measures and, finally, a card where the chosen measure is displayed via M_Measure. Then, it will look like this:
Good luck! Looking forward to the answers of other users on the second part of your question learning how to switch between different data tables!
After a lot of testing, I have arrived at the following conclusions:
Using SWITCH or similar to declare a measure to be used within CALCULATE is not valid syntax. For these types of applications, calculation groups exist, where you code your measures with a placeholder SELECTEDMEASURE.
Arbitrarily shaped filters are allowed in DAX, but apparently not conditionally. It appears passing filter tables to CALCULATE conditionally, either by IF or SWITCH throws errors. In dax.do this error points to a lack of column declaration, but this is not needed for a filter table passed outside of a conditional construct. I suspect this is due to data lineage not being preserved through a such a construct.

Ignore one of the filters PowerBI sends to SSAS

I need to calculate pareto but in graph we can only see top 10 items but pareto should be calculated from all items in the selected date period. So e.g. if I have date 1.10.2021 and I have 20 values for that date. I can display only 10 in powerbi, how can I ignore filter for TOPN10 in dax and calculate pareto from 20 values (in graph it wont show 100 percent for pareto calculation)
I am posting filters from PowerBI
DEFINE
VAR __DS0FilterTable =
TREATAS({FALSE,
BLANK()}, 'Breakdown'[Less then 30 sec])
VAR __DS0FilterTable2 =
TREATAS({TRUE}, 'Breakdown'[IsReportedInterval])
VAR __DS0FilterTable3 =
TREATAS({"Reported"}, 'Reported Filter'[Reported])
VAR __DS0FilterTable4 =
TREATAS({DATE(2021, 10, 1)}, 'Date'[Date])
VAR __DS0FilterTable5 =
TREATAS({"Morning"}, 'Shift'[Shift Name])
VAR __SQDS0BodyLimited =
TOPN(10, __SQDS0Core, [Breakdown__min_], 0)
Here is Pareto calculation - If I use ALLSELECTED pareto is calculated from rows filtered by powerbi filters, when I use ALL it will remove all filters which is not correct because I would get sum of all rows excluding date filter. Any ideas ?
Pareto Breakdown Description:=
VAR TotalQuantity =
CALCULATE ( SUM ( Breakdown[DurationSeconds] ), ALLSELECTED( Breakdown ) )
VAR AllBreakdowns =
SUM ( Breakdown[DurationSeconds] )
VAR SummarizedTable =
SUMMARIZE (
ALLSELECTED ( Breakdown ),
'Breakdown'[Description SK],
"Amount", SUM ( Breakdown[DurationSeconds] )
)
VAR CumulativeSum =
SUMX ( FILTER ( SummarizedTable, [Amount] >= AllBreakdowns ), [Amount] )
RETURN
DIVIDE( CumulativeSum, TotalQuantity )
It will sound maybe little bit silly but I found a workaround - so I removed the filter to show only top 10 rows and within the graph I changed minimum category width which at the end shows only 10 bars (10 are hidden)

Calculate sum of a column in power BI depending on a condition that should be evaluated over each row

I have a table which looks like this:
I need to calculate sum of all numbers in column :ADD , IF the number in column CHANGE is 0 AND column DELETE is 0.
The actual data looks like below. Its same as above. The items with red backets should be filtered out because there are values against delete and change for each unique number in MEMO column.
Create a Measure as below to Exclude records from the list-
exclude =
VAR current_row_tsframe_account = MIN(your_table_name[TSframe_Account])
VAR current_row_tsframe_memo = MIN(your_table_name[TSframe_Memo])
VAR find_delete_change_for_memo =
CALCULATE(
COUNT(your_table_name[TSframe_Memo]),
FILTER(
ALL(your_table_name),
your_table_name[TSframe_Memo] = current_row_tsframe_memo
&& your_table_name[TSframe_Account] = current_row_tsframe_account
&& your_table_name[TSframe_Mode] IN {"CHANGE","DELETE"}
)
)
RETURN
IF(
find_delete_change_for_memo > 0,
"Yes",
"No"
)
The above Measure will return Yes/No per row. You can now Apply visual/page level filter so that records only show where measure Exclude = No. Now this below measure will show your expected value-
total = SUM(your_table_name[TSframe_Memo])

How to show Total Sale value Bar chart or any visual in Power BI

Can anybody please help me in getting Total Sale in one bar in Bar Chart visual as show below in picture.
I have calculated total in dataset only just to show the example.
Unlike with a table or matrix visual, to get a total on a bar chart you have to do it manually by creating a new table to use as your axis and a new measure to switch between the total and the parts.
See this community post for example.
Here's how you can create a new table to use as your axis:
ChartAxis = UNION ( VALUES ( Sales[City] ), ROW ( "City", "All" ) )
You'll also need a new measure to put on the chart:
SalesByCity =
VAR AxisCity = SELECTEDVALUE ( ChartAxis[City] )
RETURN
IF (
AxisCity = "All",
SUM ( Sales[Sales] ),
CALCULATE ( SUM ( Sales[Sales] ), Sales[City] = AxisCity )
)