Power BI - Line Chart with changing color for trend - powerbi

Just wondering if it's possible to create a chart like below.
I have created the following measures in DAX:
1. [Total Sales]
2. [PM Sale] =
CALCULATE([TotalSales], PARALLELPERIOD('Date'[Date], -1, MONTH)) // Previous month Sales
3. [Indicator] = IF([TotalSales] - [PM Sale] >=0, 1, 0)
4. [IndicatorText] = IF([TotalSales] - [PM Sale] >=0, "UP", "DOWN")
I thought adding the [Indicator] or [IndicatorText] to "Legend" property of line chart would be possible and then be able to change the color, but it is not.
Any ideas?
Thanks,
Shiv

This isn't exactly what you are requesting, but a waterfall chart works pretty well here.
Create a measure to be the difference from the last month and use that for the waterfall chart's y-axis with the date in the category section.
Diff = [Total Sales] - CALCULATE([Total Sales], PARALLELPERIOD('Date'[Date], -1, MONTH))
You can also use a regular bar chart with two series:
Up = IF([Diff] >= 0, [Total Sales], BLANK())
Down = IF([Diff] < 0, [Total Sales], BLANK())
If you convert this to a line chart, it would look like this (you need to set the x-axis to categorical instead of continuous):
It's possible to tweak the measures a bit by looking at the next month in addition to the previous month and get what you want for this particular case, but it won't work correctly in general:
You can't tell from the image, but the first red line segment is covering a green line segment. If red and green segments alternate, then this methodology breaks down.
Here are the adjusted measured for the chart above:
Forward = IF(ISBLANK(PARALLELPERIOD('Date'[Date] , 1, MONTH)),
BLANK(),
CALCULATE([Total Sales]),
PARALLELPERIOD('Date'[Date], 1, MONTH))
- [Total Sales])
Up = IF([Diff] >= 0 || [Forward] >= 0, [Total Sales], BLANK())
Down = IF([Diff] < 0 || [Forward] < 0, [Total Sales], BLANK())

Related

Conditional formatting based on condition on a measure

I have Data like this and I want to create a matrix visual. Since I need the Amount and Percentage in one column inside the matrix, I have created the following structured table in Power BI:
Description
Value
Sales Amount
50000
Sales Percentage
12%
Sales Amount
25000
Sales Percentage
25%
Sales Amount
75000
Sales Percentage
64%
Since it's not possible to store different format types in a single column in Power BI the percentage is stored as decimals and I created a measure to change the format based on the description column with the following code:
Value_formated =
VAR Val = SUM ( Table[Value] )
RETURN
SWITCH (
SELECTEDVALUE ( 'Table'[Description] ),
"Sales Amount", FORMAT ( Val, "0" ),
"Sales Percentage", FORMAT ( Val, "0.00%" ))
My question is how am I able to create a conditional formating to change the underlying color based on the percentage Value? Like for example if the percentage is negative, the percentage field should be red and if positive green. But since percentage is mixed with total number, how can I only filter the percentages? I have tried the following guide: https://xyloslearning.com/en/power-bi-using-a-measure-to-set-up-conditional-formatting/ but I couldn't select the coloring measure maybe because there are two different formats? Can anybody help me?
Thanks!
Create a format measure as follows:
Format =
VAR v = MIN ( 'Table'[Value] )
VAR d = MIN ( 'Table'[Description] )
RETURN
SWITCH(TRUE(),
d = "Sales Percentage" && v < 0, 0,
d = "Sales Percentage" && v >= 0, 1,
2)
Apply conditional formatting as follows:

DAX - SUMMARIZE based on SWITCH statement

I have SKUs data with some financial information. I have grouped the SKUs in 4 categories using a measure with SWITCH. Now I would like to summarise the information (e.g. total revenues of the products in each category) but I cannot get the summary to add up to the correct value.
Data model:
FactTable: fact table containing financial information (product code, revenue, profit, period, etc.)
DimSKU: mapping table with SKUs data (product code, brand, segment, etc.)
DimDates: date table (period, months, etc.)
Creating 4 product categories:
(using a measure and not a calculated column as the users can use a dropdown list and select different periods)
My Quadrants =
VAR
Quadrants =
SWITCH(
TRUE(),
AND([SKU_profit] >= [Total_profit], [SKU_growth] >= [Total_growth]), "Maintain",
AND([SKU_profit] < [Total_profit], [SKU_growth] < [Total_growth]), "Address",
AND([SKU_profit] >= [Total_profit], [SKU_growth] < [Total_growth]), "Grow",
AND([SKU_profit] < [Total_profit], [SKU_growth] >= [Total_growth]), "Investigate"
)
RETURN
Quadrants
Calculating YTD sales:
YTD revenue =
VAR
YTD_revenue = TOTALYTD(SUM(FactTable[Revenue]), DimDates[Date])
RETURN
YTD_revenue
Summarising data:
I can add the measure to a visual like a table and it works perfectly fine as long as I also display all the SKUs. Ideally, I would like a summary like the following:
MyQuadrants / Revenue
Maintain / 1,000
Address / 250
Grow / 500
Investigate / 350
I would be grateful if anyone could point me in the right direction.
Thanks all.
Edit
I have managed to make it work thanks to David's suggestion. See below for reference.
Creating configuration table
ConfigTable =
VAR
Total_profit = [Total_profit]
VAR
Total_growth = [Total_growth]
RETURN
{
("Maintain", Total_profit, 1, Total_growth, 1),
("Address", 0, Total_profit, 0, Total_growth),
("Grow", Total_profit, 1, 0, Total_growth),
("Investigate", 0, Total_profit, Total_growth, 1)
}
// 5 columns: quadrant, min profit, max profit, min growth, max growth
// Use table constructor to use variables
Creating measure
YTD revenue (segmented) =
CALCULATE(
[YTD revenue],
FILTER(
VALUES(DimSKU),
NOT ISEMPTY(
FILTER(
ConfigTable,
(
[SKU_profit] >= ConfigTable[min profit] &&
[SKU_profit] < ConfigTable[max profit] &&
[SKU_growth] >= ConfigTable[min growth] &&
[SKU_growth] < ConfigTable[min growth]
)
)
)
)

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 too slow - scalar value materialization problem

I have the following measure which calculates a node size for a graph chart table:
MEASURE tMeasures[m_ONA_nodeSize_flex] =
IF(
ISBLANK([m_ONA_rankFilter_nodes]),
BLANK(),
var empCnt = ROW(
"data",
CALCULATE(
SUMX( DISTINCT(tONAAppend[id]), 1),
FILTER(
ALL(tONAAppend),
NOT( ISBLANK([m_ONA_rankFilter_nodes]))
)
)
)
RETURN
IF(
empCnt > 25, ROUND( 1500 / empCnt, 0),
60
)
)
[m_ONA_rankFilter_nodes] is used to filter those nodes which exist in edges in the same table. These edges are also filtered using multiple conditions with a measure m_ONA_edgeValue_afterRank, which returns BLANK() if a row doesn't match filters and edge_value if it does.
The script before using tMeasures[m_ONA_nodeSize_flex] with included measures [m_ONA_rankFilter_nodes] and m_ONA_edgeValue_afterRank works relatively fast:
EVALUATE
TOPN(
501,
SUMMARIZECOLUMNS(
'tONAAppend'[tableName],
'tONAAppend'[name],
'tONAAppend'[nameFrom],
'tONAAppend'[nameTo],
'tONAAppend'[id],
'tONAAppend'[idFrom],
'tONAAppend'[idTo],
'tONAAppend'[photoSrc],
__DS0FilterTable,
__DS0FilterTable2,
__DS0FilterTable3,
__DS0FilterTable4,
"m_ONA_edgeValue_afterRank", 'tMeasures'[m_ONA_edgeValue_afterRank],
"m_ONA_rankFilter_nodes", 'tMeasures'[m_ONA_rankFilter_nodes]
),
'tONAAppend'[tableName],
1,
'tONAAppend'[name],
1,
'tONAAppend'[nameFrom],
1,
'tONAAppend'[nameTo],
1,
'tONAAppend'[id],
1,
'tONAAppend'[idFrom],
1,
'tONAAppend'[idTo],
1,
'tONAAppend'[photoSrc],
1
)
However, when I replace 'tMeasures'[m_ONA_rankFilter_nodes] with a 'tMeasures'[m_ONA_nodeSize_flex] it starts working dramatically slower:
EVALUATE
TOPN(
501,
SUMMARIZECOLUMNS(
'tONAAppend'[tableName],
'tONAAppend'[name],
'tONAAppend'[nameFrom],
'tONAAppend'[nameTo],
'tONAAppend'[id],
'tONAAppend'[idFrom],
'tONAAppend'[idTo],
'tONAAppend'[photoSrc],
__DS0FilterTable,
__DS0FilterTable2,
__DS0FilterTable3,
__DS0FilterTable4,
"m_ONA_edgeValue_afterRank", 'tMeasures'[m_ONA_edgeValue_afterRank],
"m_ONA_nodeSize_flex", 'tMeasures'[m_ONA_nodeSize_flex]
),
'tONAAppend'[tableName],
1,
'tONAAppend'[name],
1,
'tONAAppend'[nameFrom],
1,
'tONAAppend'[nameTo],
1,
'tONAAppend'[id],
1,
'tONAAppend'[idFrom],
1,
'tONAAppend'[idTo],
1,
'tONAAppend'[photoSrc],
1
)
As I understand, the problem is in DAX engine working: it tries to calculate the value of this measure for each row. I think the fastest algo could be calculate once, then send to storage, and then populate all rows with it.
How can I optimize and force DAX to work more efficient?
I found good articles which relate to this topic and then implemented approach with variables written there:
https://www.sqlbi.com/articles/optimizing-conditions-involving-blank-values-in-dax/
https://www.sqlbi.com/articles/understanding-eager-vs-strict-evaluation-in-dax/
It worked as expected - all measures put in special variables were materialized in a Storage Engine. The performance has increased dramatically.

DAX PowerBI: Replaced blank values with zero and issue with chart

I tried to create report in Power BI with sales month by month for last 20 months, when sales is blank I want to see month with 0 value.
I decided to change Blank() values with zero adding a 0 at the end of calculation.
It works great, however I have an issue with limitaton date hierarchy, because now my chart contains a lot of months without value (first value begins in 2017, date hierarchy first value begins in 2000).
Test:=
CALCULATE (
SUM( quantity ),
flag = 1,
title = "WEEKS"
) + 0
Instead of a plain 0, you could add an IF to specify to only add that after the first value. Something like this:
Test:=
VAR FirstDate = CALCULATE ( MIN ( date ), ALL( Dates ), quantity > 0 )
RETURN
CALCULATE (
SUM( quantity ),
flag = 1,
title = "WEEKS"
) + IF( date > FirstDate, 0 )
If the condition is false, the IF returns a blank and it shouldn't show up.