Creating a calculated field in Power BI - powerbi

I have a tricky PowerBI problem that I'm hoping for some help with.
The data are as follows:
Product
Year
Purchases
'Book'
2020
24
'Book'
2021
15
'Book'
2022
10
'TV'
2020
42
'TV'
2021
48
'TV'
2022
33
'PC'
2020
130
'PC'
2021
115
'PC'
2022
170
I need help firstly making a calculated field that shows the percentage of growth/decline in each category year on year for the two most recent years (2021 and 2022 in this case). The change should be calculated as follows:
=(MostRecentYear - PreviousYear)/PreviousYear
2021 and 2022 can be specified if required, but would prefer to use MAX and MAX-1 to determine it so that I don't need to update code every year. If that's possible of course... It would also be great if the result can be expressed as a percentage and limited to one decimal place as per the table below.
Secondly, I need to include all these data in a matrix in the following format:
Product
2020
2021
2022
% Change
'Book'
24
15
10
-33.3%
'TV'
42
48
33
-31.3%
'PC'
130
115
170
47.8%
Thanks in advance.

Define the following three measures:
Total_Purchases =
SUM ( 'Table'[Purchases] )
% Change =
VAR Most_Recent_Year =
CALCULATE (
MAX ( 'Table'[Year] ),
ALL ( 'Table'[Year] )
)
VAR Previous_Year = Most_Recent_Year - 1
VAR Purchases_Most_Recent_Year =
CALCULATE (
[Total_Purchases],
'Table'[Year] = Most_Recent_Year
)
VAR Purchases_Previous_Year =
CALCULATE (
[Total_Purchases],
'Table'[Year] = Previous_Year
)
RETURN
DIVIDE (
Purchases_Most_Recent_Year - Purchases_Previous_Year,
Purchases_Previous_Year
)
2020 =
CALCULATE ( [Total_Purchases], 'Table'[Year] = 2020 )
Duplicate the last measure as required for other relevant years. Add all measures to a standard table visual.

Related

Monthly percentage of year [PowerBi] [Dax]

I'm quite new to Power Bi and DAX in general and I have some problems calculating how much of each month was of the whole year.
Example:
Year 2021:
Month Value Percentage
Jan. 100 10
Feb. 50 5
Mar. 250 25
Apr. 30 3
Etc...
Total 1000 100
I have calculated the percentage column in dax as:
=
[Value] /
CALCULATE(
[Value],
ALLEXCEPT(Calendar, Calendar[Year])
)
This gives me the correct result for the chosen year, the problem I have is when trying to compare it to last year's result.
I've tried to add
"SAMEPERIODLASTYEAR(Calendar[Key_Calendar])" and "PARALLELLPERIOD(CALENDAR[Key_Calendar],-12,Month)"
but neither of them gives me the result I am looking for.
I'd appreciate any help that I can get on the issue.
First you need to create such a model:
If you want to create a simple calendar table, create new table and paste this code:
Calendar =
VAR BaseTable = CALENDAR(DATE(2020,01,01), DATE(2021,12,31))
RETURN
ADDCOLUMNS(
BaseTable,
"Year", YEAR([Date]),
"Month", MONTH([Date]),
"MonthName", FORMAT([Date],"mmm"),
"Quarter", QUARTER([Date]),
"Period", FORMAT([Date],"yyyy-mm")
)
Your resulting Date table:
Then create a fact table with some data:
Date Value
01/01/2021 100
01/02/2021 50
01/03/2021 250
01/04/2021 30
01/05/2021 25
01/06/2021 50
01/07/2021 65
01/08/2021 75
01/09/2021 70
01/10/2021 35
01/11/2021 40
01/12/2021 20
01/01/2020 10
01/02/2020 25
01/03/2020 65
01/04/2020 85
01/05/2020 35
01/06/2020 25
01/07/2020 35
01/08/2020 10
01/09/2020 30
01/10/2020 50
01/11/2020 30
01/12/2020 20
Then we can create 2 new measures!
Percentage =
DIVIDE(SUM(FactTable[Value]),CALCULATE(SUM(FactTable[Value]),ALL(FactTable)))
Percentage_LastYear =
CALCULATE(
[Percentage],
SAMEPERIODLASTYEAR('Calendar'[Date])
)
Then create a table visual and put it like you see in the below screenshot:
And you are Good to go! I hope It solves your problem!
Assuming that your Calendar dimension has a date column called Date, the following would get the value for the prior year:
Value PY =
CALCULATE ( [Value], SAMEPERIODLASTYEAR ( 'Calendar'[Date] ) )

How can I create a dynamic measure or column for an array in PowerBI?

Excel Example
I am attempting to recreate a similar chart in PowerBI as I did in excel seen below:
Here I have revenue per day. The chart shows the percent of days where revenue exceeds a fixed amount (100, 200, etc).
In PowerBI I know how to recreate the table that the chart is based on by defining a column, but it's not dynamic. I can't apply filters to change the column values.
I know I can apply filters to measures but when I try to replicate the formula as a measure I get an error, which I assume is due to the formula trying to return an array of values.
Here is my formula for the fixed column version:
table2 column = countx(
filter(
DayRevenueTable,
[Revenue]>Table2[DayRevenueExceeding])
,[Day])
/Total
Assuming your table looks like this:
Date
Revenue
04 January 2022
102
11 January 2022
162
17 January 2022
180
02 January 2022
185
12 January 2022
203
05 January 2022
278
01 January 2022
353
16 January 2022
449
14 January 2022
500
06 January 2022
515
08 January 2022
582
10 January 2022
600
03 January 2022
618
09 January 2022
626
13 January 2022
626
15 January 2022
706
18 January 2022
765
07 January 2022
895
You need to first create a table with your fixed values. Basically is the same concept as creating a parameter.
Using that table as a reference, you can create your calculation around Fixed Values[Value].
DAX: Fixed Values
Fixed Values = GENERATESERIES(100,1000,100)
DAX: Days When Revenue Exceeds Amount
Days When Revenue Exceeds Amount =
VAR CurrentFixedValue =
SELECTEDVALUE ( 'Fixed Values'[Value] )
VAR CountValues =
CALCULATE (
COUNTROWS ( 'Table' ),
'Table'[Revenue] < ( CurrentFixedValue + 100 )
)
VAR AllValues =
CALCULATE ( COUNTROWS ( 'Table' ), ALLSELECTED ( 'Table' ) )
VAR Calc =
DIVIDE ( CountValues, AllValues )
RETURN
Calc
Output

Power BI | circumvent "Expressions that yield variant data-type cannot..." when adding a calculated column to a summarized table

Again.
Sorry to bother, but currently, I'm trying to estimate the size of a call center, which of course, requires calculating some parameters for the Erlang-A distribution. At this time, I want to get the Average Time to Abandon, which, in fact, is the median of the abandon time, or, the abandon time up to which the lower half the abandoned calls are abandoned.
TABLE A is the result of a SELECTCOLUMN function that yields:
TABLE A
Call ID
date
YEAR
MONTH
WEEK OF THE YEAR
DAY OF THE WEEK
TIME BAND
SERVICE
TIME BEFORE ABANDON
asdf1
19-apr-2021
2021
4
17
1
11 hrs
INFO
49
asdf8
26-apr-2021
2021
4
18
1
16 hrs
INFO
57
asdf7
26-apr-2021
2021
4
18
1
16 hrs
INFO
85
asdf5
26-apr-2021
2021
4
18
1
08 hrs
INFO
103
asdf2
20-apr-2021
2021
4
17
2
12 hrs
APPOINTMENT
123
asdf4
26-apr-2021
2021
4
18
1
09 hrs
INFO
176
asdf3
26-apr-2021
2021
4
18
1
13 hrs
HOTLINE
224
asdf6
26-apr-2021
2021
4
18
1
16 hrs
INFO
296
Call ID is unique.
What I want to do is to calculate the median, for any number of "filters" combination.
For example, the GENERAL median should be 103 seconds, but, if I focus only on the calls that took place in the 16 hrs time band, the median is 85 seconds
TABLE B was created with the SUMMARIZE function applied on TABLE A. So, from TABLE A sample, TABLE B
TABLE B
SERVICE
YEAR
MONTH
WEEK OF THE YEAR
DAY OF THE WEEK
TIME BAND
-
fully filtered MEDIAN PATIENCE
MEDIAN PATIENCE (service only)
GENERAL MEDIAN PATIENCE
INFO
2021
4
18
1
16
-
85
85
103
INFO
2021
4
18
1
08
-
103
85
103
INFO
2021
4
18
1
09
-
176
85
103
INFO
2021
4
17
1
11
-
49
85
103
APPOINTMENT
2021
4
18
2
12
-
123
123
103
HOTLINE
2021
4
18
1
13
-
224
224
103
From SERVICE to TIME Band, it's the summarize part. Afterwards, it's the median(s) columns
What I want is to add the medians columns. However, for the fully filtered median, I'm attempting to do so with the code:
fully median patience = CALCULATE(MEDIAN('TABLE A'[TIME BEFORE ABANDON]), FILTER('TABLE A', 'TABLE A'[SERVICE] = 'TABLE B'[SERVICE] && 'TABLE A'[YEAR] = 'TABLE B'[YEAR] && 'TABLE A'[MONTH] = 'TABLE B'[MONTH] && 'TABLE A'[DAY OF THE WEEK] = 'TABLE B'[DAY OF THE WEEK] && 'TABLE A'[TIME BAND] = 'TABLE B'[TIME BAND]))
But I'm getting the message: "Expressions that yield variant data-type cannot be used to define calculated columns."
Which seems weird to me, since the function countrows works just fine within a CALCULATE, and applied to a bunch of summarized columns.
As a matter of fact, I can get the median with a MEASURE, insert it in a CARD VISUAL, and I only have to add the filters to it. Or add many slicers to a dashboard page. But still, It would be better to get medians in the TABLE in order visualize many medians at the time.
Any suggestions?
You result must contain some blanks that's why you are getting the error, do explicit conversion with CONVERT
fully median patience =
CONVERT (
CALCULATE (
MEDIAN ( 'TABLE A'[TIME BEFORE ABANDON] ),
FILTER (
'TABLE A',
'TABLE A'[SERVICE] = 'TABLE B'[SERVICE]
&& 'TABLE A'[YEAR] = 'TABLE B'[YEAR]
&& 'TABLE A'[MONTH] = 'TABLE B'[MONTH]
&& 'TABLE A'[DAY OF THE WEEK] = 'TABLE B'[DAY OF THE WEEK]
&& 'TABLE A'[TIME BAND] = 'TABLE B'[TIME BAND]
)
),
INTEGER
)

Dynamic Quartile based on Date Slicer Power BI

I have a sales table where I want to initially summarize by the ship to customer name in the temp table GroupTable. From there I want to return the 1st quartile value but the issue I am having is that I would like to use PERCENTILE.INC not PERCENTILEX.INC however I was not able to find a workaround considering my table I am referencing is a temp table. My end goal is to have dynamic quartiles based on a date slicer where I do not have to create a new static table.
Quantile1_Sales =
VAR GroupTable =
SUMMARIZE (
'Sales',
'Sales'[Customer Ship To],
"Sales2", SUMX ( 'Sales', 'Sales'[Sales] )
)
RETURN
PERCENTILEX.INC ( GroupTable, [Sales2], .25 )
Here is an example of what the Sales table looks like below.
You should calculate your table inside the PERCENTILEX function.
See the calculation below where it takes all the selected values for Customer Ship To and Date.
Calculation: Measure
Quantile1_Sales =
PERCENTILEX.INC (
ALLSELECTED ( 'Table'[Customer Ship To], 'Table'[Date] ),
CALCULATE ( SUM ( 'Table'[Sales] ) ),
0.25
)
Output
Table Reference: Table
Date
Customer
Customer Ship To
Sales
01 December 2020
Customer A
CustA1
100
01 December 2020
Customer A
CustA1
200
30 December 2020
Customer B
CustB1
500
01 January 2021
Customer C
CustC2
300
01 January 2021
Customer D
CustD1
100
02 January 2021
Customer D
CustD2
150
04 January 2021
Customer A
CustA2
200
01 May 2021
Customer D
CustD1
100
01 June 2021
Customer F
CustF2
50

How do you use Data Analysis Expression (DAX) to Rank number based on Week number of Year?

I am looking to Dense Rank Amount of Clicks based on the week number of each year using Power BI.
The problem that I am running into is that the weeknumber is the same in the next year... and I cannot seem to get the DAX to sequence in a row correctly. Any ideas on what the DAX script would look like is appreciated.
[CODE]
The output would look something like this:
YearNumber WeekNumber NumberOfClicks RankNumClicks
2020 1 362 8
2020 1 325 9
2020 1 421 5
2020 1 224 12
2020 1 125 14
2020 1 440 2
2020 1 321 11
2020 2 224 12
2020 2 136 13
2020 2 423 4
2020 2 110 15
2020 2 431 3
2020 2 521 1
2020 2 324 10
2020 3 368 7
2020 3 412 6
2020 3... and so on.
Here is what I think the DAX script should be like and what I have so
far: (Please note that I used SalesAmount for number of clicks
testing):
Ranking =
VAR WeekNumberOfYear = ISFILTERED( DimDate[WeekNumberOfYear] )
VAR FiscalYear = ISFILTERED( DimDate[CalendarYear] )
RETURN
IF( ISBLANK( 'FactInternetSales'[SaleAmounts] ), BLANK(),
IF(FiscalYear,
RANKX( ALLSELECTED( DimDate[CalendarYear] ),
'FactInternetSales'[SaleAmounts], , DESC, Dense ),
IF( NOT( WeekNumberOfYear),
RANKX( ALLSELECTED(DimDate[CalendarYear] ),
'FactInternetSales'[SaleAmounts], , DESC, Dense ),
BLANK())) )
[/CODE]
You can use newly introduced Hierarchy Slicer (go to Options and Settings, select Options, then Preview features under GLOBAL and check Hierarchy slicer).
Then, you can easily filter data based on specific Year/Week.
Hope this helps.