Power BI Totals evaluation - powerbi

I have the below table with many values that gives me a total PerecntageChange too.
Date CheckType State Previous Daily PercentChange KPI light
6/13/21 AM AR 29489 33023 11.98% Yellow
6/13/21 AM KY 105496 104648 -.80% Green
I need to create seperate KPI light metrics for totals. How can I do this?
Measure
Percentage change =
VAR __BASELINE_VALUE = SUM('AutoDialerDaily'[PREV_CNT])
VAR __VALUE_TO_COMPARE = SUM('AutoDialerDaily'[CURR_CNT])
RETURN
IF(
NOT ISBLANK(__VALUE_TO_COMPARE),
DIVIDE(__VALUE_TO_COMPARE - __BASELINE_VALUE, __BASELINE_VALUE)
)
Column Color Indicator that drives the KPI light (Conditional Formatting)
COLOR_IND_FORMAT =
//IF(HASONEVALUE(AutoDialerDaily[State]),
SWITCH(
TRUE(),
AutoDialerDaily[Check type] = "STATE AM" && AutoDialerDaily[PRCNT_DIFF] >=16, "RED",
AutoDialerDaily[Check type] = "STATE AM" && AutoDialerDaily[PRCNT_DIFF] >=11 && AutoDialerDaily[PRCNT_DIFF] <=15 , "YELLOW",
AutoDialerDaily[Check type] = "STATE AM" && AutoDialerDaily[PRCNT_DIFF] <=10, "GREEN",
"BLACK")
I'm not sure what needs to happen here.
I want a separate set of "TOTAL Metrics" and to control the color. So, for example if I wanted to say if the Total is 2% that should display as a "BLUE" indicator

You can create a measure as below:
COLOR_IND_FORMAT =
SWITCH(
TRUE(),
VALUES('Table'[CheckType]) = "AM" && [Percentage change] >= 0.10, 1,
VALUES('Table'[CheckType]) = "AM" && [Percentage change] < 0.10, 2)
After this, select the "Conditional Formatting" option from the Format Pane. Select the "Percentage Change" column in the dropdown > Go to Icons > Advanced Controls
In the Advanced Controls box, you can format based on the measure you created. Please refer to the image below:
Final Output:
Hope this solves your problem.

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]
)
)
)
)

How to calculate sum with multiple conditions in power bi

I would like to create a DAX formula with a IF statement.
my logic would be :
IF(column[1]= "sales" && column[2] ="chicago"; SUM('Table'[SalesAmount]);
IF(column[1]= "sales" && column[2] ="sanfranciso"; SUM('Table'[SalesAmount]);
IF(column[1]= "sales" && column[2] ="newyork"; SUM('Table'[SalesAmount]);
IF(column[1]= "sales" && column[2] ="hoston"; SUM('Table'[SalesAmount]);
So, i need to calculate sales by city. how can we write above logic in dax expression in power bi?
You can use the CALCULATE function with your conditions.
For example, let's use it to calculate the sales amount of chicago
chicago_sales_amount = CALCULATE(SUM('Table'[SalesAmount]);column[1]= "sales" && (column[2] = "chicago" || column[2] = "sanfranciso" || column[2] = "newyork" || column[2] = "hoston"))
This above expression will calculate the sum of sales, only for the rows that respect the given condition.
Now you can apply the same logic for the other condition's.
CALCULATE documentation
If you want to get the sum by city but only want it when column[1] = "sales" you can summarize based on a filter:
SumByCity =
VAR curCity = 'Table'[column[2]]
RETURN
CALCULATE(SUM('Table'[SalesAmount]), FILTER(curCity = 'Table'[column[2]] && 'Table'[column[1]]= "sales"))

QoQ changes by Customer in DAX

I would like to create a DAX formula to calculate the increases and decreases of customers across periods. Following is a sample of the data that I have
Year-Quarter|Customer|Credit-Limit
2019Q2|A|50
2019Q2|B|100
2019Q2|C|100
2019Q2|D|200
2019Q2|E|1000
2019Q2|F|200
2019Q3|A|50
2019Q3|B|200
2019Q3|C|100
2019Q3|D|50
2019Q3|E|500
2019Q3|F|300
I am looking to create a summary by Year-Quarter showing the number of customers that had an increase/decrease/none of their Credit-Limit.
Note that this is just a sample and the actual data is >10M rows. So even though I can create another table, I think from a computation standpoint, a measure would be more useful
Desired Output:
A commentary like the following: "There are 2 customers that have increased credit limit in 2019Q3"
Done so far:
Prev Quarter Credit Limit =
VAR CurrentYearQuarter = MAX(Sheet1[Year-Quarter])
VAR Quarter_Year =
LEFT (CurrentYearQuarter, 4)
VAR Quarter_period =
RIGHT (CurrentYearQuarter, 1 )
RETURN
IF (
Quarter_period = "1",
CALCULATE (
SUM ( Sheet1[Credit Limit] ),
Sheet1[Year-Quarter]
= ( Quarter_Year - 1 )
& "Q"
& ( Quarter_period + 3 )
),
CALCULATE (
SUM ( Sheet1[Credit Limit] ),
Sheet1[Year-Quarter]
= Quarter_Year
& "Q"
& Quarter_period - 1
)
)
Inc/Dec = IF(SUM(Sheet1[Credit Limit]) - [Prev Quarter Credit Limit] > 0,"Inc",
IF(SUM(Sheet1[Credit Limit]) - [Prev Quarter Credit Limit] < 0,"Dec","None"))
Commentary = "There are " &
CALCULATE(DISTINCTCOUNT(Sheet1[Customer]),
FILTER(Sheet1, [Inc/Dec] = "Inc" && Sheet1[Year-Quarter] = "2019Q3"))
Current output:
Commentary: "There are 4"
I am not sure why I am getting 4 as compared to 2 as the number here. Help or guidance would be really appreciated
I've slightly altered the input data for experimental purpose, I've added
2018Q3 | A | 200
2018Q4 | A | 50
2019Q1 | A | 50
I've added a Quarter-Calendar (which is a calculated table using VALUES(Sheet1[Year-Quarter])
Then by adding more columns to this new table, extracting the current year and quarter using LEFT and RIGHT, then calculating the previous quarter, previous year and combining to a Prevoius-Year-Quarter column:
]
Using this Q-Calendar I create a 1:* relationship to the Sheet1 table between [Year-Quarter] and [Year-Quarter], then I create a second inactive 1:* relationship between [Previous-Year-Quarter] and [Year-Quarter] like this:
I then create two measures, one for sum of previous quarter credit an one for current quarter credit:
Current-Quater CL =
var currentQ = MAX('Q-Calendar'[Year-Quarter])
var tempTable =
FILTER(
ALL('Q-Calendar');
'Q-Calendar'[Year-Quarter] = currentQ
)
return
CALCULATE(
SUM('Sheet1'[Credit-Limit ]);
tempTable
)
In the [Previous-Quarter CL] measure I use USERELATIONSHIP to activate the passive relationship I added from the Q-Calendar.
Prev-Quater CL =
var currentQ = MAX('Q-Calendar'[Year-Quarter])
var tempTable =
FILTER(
ALL('Q-Calendar');
'Q-Calendar'[Year-Quarter] = currentQ
)
return
CALCULATE(
SUM('Sheet1'[Credit-Limit ]);
tempTable;
USERELATIONSHIP('Sheet1'[Year-Quarter]; 'Q-Calendar'[Previous-Year-Quarter])
)
Then create the Inc/Dec measure like this:
Increase/Decrease =
var temp = [Current-Quater CL]-[Prev-Quater CL]
return
SWITCH(
TRUE();
temp > 0; "Increase";
temp < 0; "Decrease";
"No change"
)
And finally created a new (actually 3 new) Commentary measures like this:
Commentary_2 = "There are " &
var customers = VALUES(Sheet1[Customer])
return
SUMX(
customers;
CALCULATE(
IF(
[Current-Quater CL]-[Prev-Quater CL] > 0;
1;
0
)
)
)&" customers how have increased their credit"
Using the Year-Quarter column from the Q-calendar as a slicer I get the current status and I can select a previous quarter to see the status at that time:
N.B: The code in my measures can be optimised, I just kept them as detailed as this to make it more understandable.

Power BI - Line Chart with changing color for trend

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())