PowerBi - Counting Switch Statements - powerbi

I have the following measure:
test = SWITCH(TRUE(),
MAX(test[month])>=9&&MAX(test[month])<=12,"fall",
MAX(test[month])>=1&&MAX(test[month])<=3,"winter",
MAX(test[month])>=4&&MAX(test[month])<=6,"spring",
MAX(test[month])>=7&&MAX(test[month])<=8,"summer")
Currently it looks at the month number (i.e. "3" for March and outputs "winter", what I'd like however is it to output is a count per season to show the distribution of the seasons in the dataset.
For example my desired output would be
Month Number
Count of occurrences of each season
fall
5
winter
7
spring
11
summer
2
I can't have a calculated column here either as I will want to make this measure dynamic later on with the use of a slicer, can someone tell me if this is possible?

The issue here is that you want to define your categories within the measure. Measures are not dynamic without some filter-context.
Take this for example:
Notice that the output of the calculation is identical between seasons.
There is no filter context to help the measure discern between the different seasons because these seasons are not defined in the model. (At least, I don't know how to make this work)

Switch returns the first true result. So, if you have values like in your sample, then start with the smallest, then bigger, and the largest at the end.
test =
SWITCH(
TRUE()
,MAX(test[month])<4,"winter" -- test <4
,MAX(test[month])<7,"spring" -- 3< test < 7
,MAX(test[month])<9,"summer" -- 6< test < 9 -- Is it ok that you have 2 months in
,"fall" -- 8< test -- summer and 4 in fall?
)
If you use MAX(test[month])<4,"winter" instead of MAX(test[month])<=3,"winter" then you avoid one calculation step and the code will be faster.
Then you need to use the result to find months numbers and get dates from the selected months. Then calculate your table filtered by months dates. If this answer is not enough to solve the case, then give more information about you table, it's columns, and what do you mean by 'Count of occurrences of each season', exactly what does 'occurrences' mean, is it a number of certain rows or some unique values.

Related

PowerBI dynamic filter for period

I would need a report that shows variances for the current period, next period, half-year and year-end.
There is a period filter from 1 to 12 representing the periods in a year.
I can build up a table that shows the difference:
for the current period (can be amended based on the period filter)
for full-year (using ALL function to ignore the period filter)
But I don't know how to write a dax that filter just for the first 6 periods as half-year.
Or add one to the current period filter to shows the next period-related variance.
Also, that should be dynamic. This means if I adjust the period filter it should show always be the next period. (If the filter is set for 1 to 4 it returns 1 to 5; set 2 to 7 returns 2 to 8).
Could you please help me with how to do that?
Thanks
So, let's say we have 2 databases.
The plan for the current year and the actuals:
Excel
And I would like to see the difference between them in one visualization.
If the period set is 1 to 2:
for the current period shows: sum plan (1-2) - sum Actual (1-2)
next period: sum plan (1-3) - sum Actual (1-3)
Half Year: sum plan (H1) - sum Actual (H1)
FY: sum plan (1-12) - sum Actual (1-12)
I am not able to understand exact query but it seems that as per your given examples it's more related to time intelligence formulas in PowerBi to given results based on specific time frames. (YTD, QTD etc)
Below is the link to address your query via time intelligence formula
https://www.analyticsvidhya.com/blog/2020/08/time-intelligence-power-bi/

Ranking in Tableau Based on another Field

How to rank based on a field where the value is the same. There is some more ranking applied here and this scenario need to be addressed as well as I can not rank again by Sales field. Instead, I need to say:
If Unit is the same on the list of territories, rank based on Sales.
Example:
Terr Sales Unit Should look like : Terr Sales Unit
---- ------ ----- -----------------------
Boston 1 5 Maine 10 5
Maine 10 5 Boston 1 5
Often a mathematical approach works well for this. First, without wanting to patronise, it's possible to use a discrete (blue) measure to sort data. Place the sorting pill to the far left on the Rows and the table will sort according to this pill.
Ok, so the formula. Without knowing how large the Sales figure can go, you want to create a calculation that would give the highest value to that you want to appear top.
For example perhaps multiple Unit by 1,000,000 and add Sales. Just make sure the Units are multiplied by a number large enough to make Sales inconsequential.
This field may work, depending how large the Sales figure can go:
[SortField] = (SUM([Unit])*1000000 + SUM([Sales])) * -1
Put the field to Rows, convert to Discrete, then place to the far left. If the sorting is correct hide the field header.
It multiplies by -1 to sort descending.

Quicksight not showing months with no data (null?)

My visual's x-axis is Date, aggregated by month, and the y-axis is total sales (sum of costs). For some months, I had no sales, but Quicksight ignores these and only shows me the months that do have data in them, even though I want the exact number of months that I specify in my time range parameter.
So, when I ask to show the last 12 months, Quicksight shows me only 4, since for the other 8 I don't have any records.
How can I make those months show as 0, instead of being excluded altogether?
---------- EDIT (possible solution) --------------
Since my analysis relied on selecting a specific product code and only getting the sums for that, I took off the filter, and instead used a calculated field of:
sumIf(sales, productCode = $productCodeParam$).
I imagine this approach works on really small datasets too if you want to use sumIf and list all of your categories there, but otherwise, by omitting data with filtering you have nothing to result in 0, since Quicksight doesn't evaluate sum(null + null) into 0.

Power BI: Calculating STDEVX.P over 6-Month period

I am attempting to calculate the most recent 6-Month STDEVX.P (not including the current month; so in May 2017, I'd like to the STDEVX.P for periods Nov 2016 - Apr 2017) for sales by product in order to further calculate variation in sales orders.
The Sales Data is made up of daily transactions so it contains transaction date: iContractsChargebacks[TransactionDate] and units sold: iContractsChargebacks[ChargebackUnits], but if there are no sales in a given period, then there will be no data for that month.
So, for example, on July 1st, sales for the past 6 months were the following:
Jan 100
Feb 125
Apr 140
May 125
Jun 130
March is missing because there were no sales. So, when I calculate STDEVX.P on the data set, it is calculating it over 5 periods, when in fact there were 6, just one happens to be zero.
At the end of the day, I need to calculate STDEVX.P for the current six month period. If when pulling the monthly sales numbers, it only comes back with 3 periods(months), then it needs to assume the other 3 periods with a zero value.
I thought about manually calculating standard deviation instead of using the DAX STDEVX.P formula and found these 2 links as a reference on how to do so, the first being closest to my need:
https://community.powerbi.com/t5/Desktop/Problem-with-STDEV/td-p/19731
Calculating the standard deviation from columns of values and frequencies in Power BI...
I attempted to make a go of it, but still am not getting the correct calculation. My code is:
STDEVX2 =
var Averageprice=[6M Sales]
var months=6
return
SQRT(
DIVIDE(SUMX(
FILTER(ALL(DimDate),
DimDate[Month ID]<=(MAX(DimDate[Month ID])-1) &&
DimDate[Month ID]>=(MAX(DimDate[Month ID])-6)
),
(iContractsChargebacks[SumOfOrderQuantity]-Averageprice)^2),
months
)
)
*note: Instead of using date parameters in the code, I created a calculated column in the date table that gives each Month a unique ID, makes it easier for me.
Your question would definitely be easier to answer with more explanation regarding your model. E.g. how you defined [SumOfOrderQuantity] and [6M Sales], since a mistake there could definitely impact the final result. Also, knowing what the result you're seeing is vs. the result you expect would be helpful (using sample data).
My guess, however, is that your DimDate table is a standard date table (with one row per date), but you want standard deviation by month.
The FILTER statement in your formula limits the date range to the prior 6 full months correctly, but it will still have one row per date. You can confirm this in Power BI by going into the Data View, selecting 'New Table' under Modeling on the ribbon, and putting your FILTER statement in:
Table = FILTER(ALL(DimDate),
DimDate[MonthID]<=(MAX(DimDate[MonthID])-1) &&
DimDate[MonthID]>=(MAX(DimDate[MonthID])-6))
Assuming you have more than one day of sales for a given month, calculating the variance by day rather than by month is going to mess things up.
What I'd suggest trying:
Table = FILTER(SUMMARIZE(ALL(DimDate),[MonthID]),
DimDate[MonthID]<=(MAX(DimDate[MonthID])-1) &&
DimDate[MonthID]>=(MAX(DimDate[MonthID])-6))
The additional SUMMARIZE statement means that you only get one row for each MonthID, rather than 1 row for each date. If your [6M Sales] is the monthly average across all 6 months, and [SumOfOrderQuantity] is the monthly sum for each month, then you should be set to go calculating the variance, squaring, dividing by 6, and square rooting.
If you need to do further troubleshooting, remember you can put a table on your canvas with MonthID, SumOfOrderQuantity and [6M Sales] and compare the numbers you expect at each stage of the calculation with the numbers you're seeing.
Hope this helps.
I was facing a similar problem while trying to calculate the coefficient of variation (Std. /Mean) by SKUS from sales data. I could use the Pivot-Unpivot function in Power Query editor to to do away with the problem of months with missing sales:
1) Export the data with any calculated columns
2) Reimport the data so that the calculated columns are also available in the power query editor
3) Pivoted the data by months
4) Replaced null values with 0s
5) Unpivoted the data
6) Close and apply the query
7) Add a calculated column for the coefficient of variation using the formula 
CV = CALCULATE(STDEV.P(Table1[Value]),ALLEXCEPT(Table1,Table1[Product]))/CALCULATE(AVERAGE(Table1[Value]),ALLEXCEPT(Table1,Table1[Product]))
Thus zero sales for the missing months will also be considered both for Standard Deviation and Mean.

Power BI Percentage of Month passed

I would like to create a calculated column or measure that shows the time elapsed in any given month. Preferably as a percentage.
I need to be able to compare productivity (rolling total) over a month's period between different months.So creating a percentage of time passed in a month would put every month on a level playing field.
Is there a way to do this?
Or is there a better way to compare productivity between 2 months on a rolling basis?
EDIT
I am graphing sales on a cumulative basis. Here is a picture of my graph to demonstrate.[][
Ideally I would like to be able to graph another person's sales on the same graph for a different month to compare.
The problem is each month is different and I don't think power bi allows much customization of the axes.
So I figured a potential solution would be to convert months to percentages of time passed, create two separate graphs and place them on top of each other to show the comparison of sales.
Using percentages doesn't sound right here: one person's "productivity" in February will appear lower than another person's productivity in March just because February has 3 less days.
Just use [Date].[Day].
To answer the original question (even though it shouldn't be used for this), month progress percentage calculated column:
MonthProgress% =
var DaysinMonth = DAY(
IF(
MONTH(MyTable[date]) = 12,
DATE(YEAR(MyTable[date]) + 1,1,1),
DATE(YEAR(MyTable[date]), MONTH(MyTable[date]) + 1, 1)
) - 1
)
return MyTable[date].[Day]/DaysinMonth*100
Also check DAX functions PARALLELPERIOD and DATEADD.
This is the solution I settled on.
I customized the ranges for the x and y axes so they match.
For the y-axis, I simply put the range from 0 to 50+ our highest month.
For the x-axis, I created a column with the DAY function so I got a number assigned to each day of the month which allowed me to manually set the chart range from 0 to 31. I have asked another question on how to only get workdays.