Power BI How to Rank based on selected parameter - powerbi

I'm working on a report and I want to give flexibilty to the user such that each time the
user select a certain product from the slicer "Products", and chooses a desired parameter from the "Metrics Value", he's going to have cards of top (country, buyertype, month and day) based.
I've tried effort and found that parameters can't be included in DAX measures, hence,
I've resolved to using if statement below to control my selection,
TopT = IF(
SELECTEDVALUE('Values'[Product SlicerP Order]) = 0,
RANKX(ALLSELECTED('Mediterranean Cameras Company'[Product]), [Net Sales],,DESC) <= 1,
IF(
SELECTEDVALUE('Values'[Product SlicerP Order]) = 1,
RANKX(ALLSELECTED('Mediterranean Cameras Company'[Product]), [Net Profit],,DESC) <= 1,
IF(
SELECTEDVALUE('Values'[Product SlicerP Fields]) = 2,
RANKX(ALLSELECTED('Mediterranean Cameras Company'[Product]), [Net Profit Margin],,DESC) <= 1,
IF(
SELECTEDVALUE('Values'[Product SlicerP Fields]) = 3,
RANKX(ALLSELECTED('Mediterranean Cameras Company'[Product]), [Total Unit Sold],,DESC) <= 1))))
but its yielded no result.

Related

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

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

DAX: How to construct a "piecewise" measure?

I have a time series that looks very different before and after a certain date.
I have one measure that calculates the average value of the time series BEFORE that date, and another measure for the average value AFTER that date.
How do I melge them into 1 measure so that when I make a line chart in my front end tool, I see the one average before, and the other average after?
Got it! Trick was using HASONEVALUE and VALUES in tandem to get "data type" agreement. The "combined" measure now displays the piecewise average I was after.
EventCount:= DISTINCTCOUNT([Event_Id])
Avg Daily Event Count Pre:= calculate([EventCount] / DISTINCTCOUNT([EventDate]), Event[EventDate] <= date(2018, 3, 1))
Avg Daily Event Count Post:= calculate([EventCount] / DISTINCTCOUNT([EventDate]), Event[EventDate] >= date(2018, 4, 1))
Avg Daily Event Count Combined:= if (
HASONEVALUE(Event[EventDate]),
if(
values(Event[EventDate]) <= date(2018, 3, 1),
[Avg Daily Event Count Pre],
if(
values(Event[EventDate]) >= date(2018, 4, 1),
[Avg Daily Event Count Post],
blank()
)
)
)

How to break down date range by each month in DAX Power BI?

I have two dates:
'2018-01-05' and '2019-01-05'
How to create calculated table to break down those dates by month.
Should look simething like that:
There are probably many ways to do this, but here's one way that combines a few different concepts:
Table =
VAR Starting = DATE(2018, 1, 5)
VAR Ending = DATE(2019, 1, 5)
VAR MonthTable =
SUMMARIZE(
ADDCOLUMNS(
CALENDAR(Starting, Ending),
"StartDate", EOMONTH([Date], 0) + 1),
[StartDate],
"EndDate", EOMONTH([StartDate], 0) + 1)
RETURN UNION(
ROW("StartDate", Starting, "EndDate", EOMONTH(Starting, 0) + 1),
FILTER(MonthTable, [EndDate] < Ending && [StartDate] > Starting),
ROW("StartDate", EOMONTH(Ending, -1) + 1, "EndDate", Ending)
)
Basically, you start with the CALENDAR function to get all the days, tag each date with its corresponding month, and then summarize that table to just return one row for each month.
Since the first and last rows are a bit irregular, I prepended and appending those to a filtered version of the summarized month table to get your desired table.
Create new table as
Table = CALENDAR( DATE(2018, 5, 1), DATE(2019, 1, 5) - 1)
Rename auto-generated column "Date" into "Start Date". Add new column as
End Date = Start Date + 1

How to check if current start and end dates are in previous period (start and end dates)

I have a tricky problem I am working on. I've made several attempts at capturing the data but have been unsuccessful.
I have a table in Power Bi that looks like this:
The key is in ascending order as well as the StartDate field. I would like to achieve the results in the "Period Overlap Delta" field but have had trouble trying to figure it out.
Basically, I'd like to assign a value of zero to any period (startdate-enddate combination) that is IN a previous period and take the date difference of those "super" periods.
Here is the DAX to produce the table:
Cases = DATATABLE("Key", integer, "StartDate", datetime, "EndDate", datetime
,{
{1, "01/01/2018", "01/10/2018"}
, {2, "01/03/2018","01/03/2018"}
, {3, "01/05/2018","01/07/2018"}
, {4, "01/15/2018","01/16/2018"}
, {5, "01/21/2018","01/24/2018"}
, {6, "01/25/2018", "01/27/2018"}
, {7, "01/25/2018","01/27/2018"}
})
Thanks so much in advance!!!
We need to know whether or not a certain row is overlapped by a previous row. By previous we mean the key is smaller than current row. By overlapped we mean the StartDate is earlier or equal to current row and EndDate is later or equal to current row, hence:
Overlapped =
COUNTROWS(
FILTER(
'Cases',
'Cases'[StartDate] <= EARLIER('Cases'[StartDate]) &&
'Cases'[EndDate] >= EARLIER('Cases'[EndDate]) &&
'Cases'[Key] < EARLIER('Cases'[Key])
)
)
And with this we just need to wrap it up and calculate the number of days with the DATEDIFF function:
Period Overlap Delta =
VAR Overlapped =
COUNTROWS(
FILTER(
'Cases',
'Cases'[StartDate] <= EARLIER('Cases'[StartDate]) &&
'Cases'[EndDate] >= EARLIER('Cases'[EndDate]) &&
'Cases'[Key] < EARLIER('Cases'[Key])
)
)
RETURN
IF(Overlapped, 0, DATEDIFF('Cases'[StartDate], 'Cases'[EndDate], DAY) + 1)
P.S. The use of DATATABLE to provide sample data is golden and should be promoted more often!