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
Related
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] ) )
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.
I have below Customer Transactions data. In a month, customer may buy one or multiple times data pack ,or may not buy data pack.
Irrespective of how many times data purchased in a month by a customer. I'm trying to find number of months that each customer purchased the data.
Total Subscribed Months is expected column.
Since customer may buy Data Subscribed (GB) more than once in a month. First I'm calculating Total Data Purchased in a month by customer.
Total Data Purchased = CALCULATE(
SUM('Customer Transactions'[Data Subscribed (GB)]),
ALLEXCEPT('Customer Transactions','Customer Transactions'[Account Number],'Customer Transactions'[Date])
)
Second, Calculate number of months customer purchased the data pack.
Total Subscribed Months =
CALCULATE(
DISTINCTCOUNT('Customer Transactions'[Account Number] ),
'Customer Transactions'[Total Data Purchased]>0
)
But its not working. Please advise how to correct formulae ?
Assuming your table looks like this:
Account Number
CUSTOMER_TYPE
Data Subscribed(GB)
Free Data
Date
Total Subscribed Months
10001
Retail
250
0
01 October 2019
1
10001
Retail
0
100
01 November 2019
1
10002
Retail
200
0
01 October 2019
1
10002
Retail
250
0
01 November 2019
2
10003
Retail
300
0
01 October 2019
2
10003
Retail
0
0
01 October 2019
2
10003
Retail
100
0
01 October 2019
2
10003
Retail
100
0
01 November 2019
2
10003
Retail
100
0
01 November 2019
2
10003
Retail
0
0
01 December 2019
2
DAX Calculations
Total Subscribed Months
Total Subscribed Months =
CALCULATE (
DISTINCTCOUNT ( 'Customer Transactions'[Date] ),
FILTER ( ALL ( 'Customer Transactions'[Data Subscribed(GB)] ), [Data Subscribed(GB)] > 0 )
)
Total Data Purchased
Total Data Purchased =
SUM('Customer Transactions'[Data Subscribed(GB)])
Output
Table visual with Account Number, Total Subscribed Months and Total Data Purchased.
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
I have a data set that contains sales forecast data by year over 5 years.
Each row has customer, item type, year, qty and sales price.
Not all customers buy all products in all years.
I want to get a list of all products that are purchased in all of the listed years.
An example, cut-down table looks like this:
Customer Product Year Qty Price
CustA ProdA 2020 50 100
CustA ProdA 2021 50 100
CustA ProdA 2022 50 100
CustA ProdB 2020 50 100
CustA ProdB 2021 50 100
CustA ProdC 2021 50 100
CustA ProdC 2022 50 100
CustA ProdD 2020 50 100
CustA ProdD 2021 50 100
CustA ProdD 2022 50 100
CustB ProdA 2021 50 100
CustB ProdA 2022 50 100
CustB ProdC 2020 50 100
CustB ProdC 2021 50 100
CustB ProdC 2022 50 100
CustB ProdD 2020 50 100
CustB ProdD 2021 0 100
CustB ProdD 2022 50 100
And transposed, looks like this:
Customer Product 2020 2021 2022
CustA ProdA 50 50 50
CustA ProdB 50 50
CustA ProdC 50 50
CustA ProdD 50 50 50
CustB ProdA 50 50
CustB ProdC 50 50 50
CustB ProdD 50 0 50
So, for this example, I'd want to do calculations on, or indicate rows that have a sales qty for all three years. I was trying to use the following formula which I would have compared with the max number of years in the set to mark a row as valid or not, but it's killing Excel. There are only 32,000 rows in the source table.
=CALCULATE(
DISTINCTCOUNT(DataTable[Year]),
filter(DataTable, DataTable[Product] = EARLIER(DataTable[Product])),
filter(DataTable, DataTable[Customer] = EARLIER(DataTable[Customer])),
filter(DataTable, DataTable[Qty] > 0)
)
Is there a better approach I could use for this?
How about this?
ProductList =
VAR AllYears = DISTINCTCOUNT ( 'DataTable'[Year] )
VAR Summary =
SUMMARIZE (
'DataTable',
'DataTable'[Product],
"YearsPurchased", CALCULATE (
DISTINCTCOUNT ( 'DataTable'[Year] ),
'DataTable'[Qty] > 0
)
)
RETURN
SELECTCOLUMNS (
FILTER ( Summary, [YearsPurchased] = AllYears ),
"Product", [Product]
)
The Summary aggregates at the Product level and looks at how many distinct years it had with non-zero quantity. Then you just filter for the ones that match AllYears and take the Product column.
Note that this returns a single column table and thus doesn't work as a calculated column or measure but a list is what you asked for.
Edit: To get the YearsPurchased as a calculated column, you just need part of this:
YearsPurchased =
CALCULATE (
DISTINCTCOUNT ( 'DataTable'[Year] ),
FILTER ( ALLEXCEPT ( 'DataTable', 'DataTable'[Product] ), 'DataTable'[Qty] > 0 )
)
You dont need to use dax to achieve this. Create a matrix visualization using the needed data.
It should looks like this:
Remember to disable the total and subtotal options.
This is other solution using a new column so you dont have to expand the matrix.
Column = COMBINEVALUES( " ", Table[Customer], Table[Product] )
Hope it helps you.