Dear stackoverflow, please help!
I'm hoping for some assistance with data processing in Power BI, either using Power Query or DAX. At this point I am really stuck and can't figure out how to solve this problem.
The below table is a list of sales by Product, Month, and Year. The problem with my data is that the value in the sales data is actually cumulative, rather than the raw figure of sales for that month. In other words, the figure is the sum of the number of sales for the month (for that Year and Product combination) and the number of sales for the preceding month. As you will see in the table below, the number gets progressively larger in each category as the year progresses. The true number of sales for TVs in Feb of 2021, for example, is the sales figure of 3 minus the corresponding figure for sales of TVs in Jan of 2021 (1).
I really would appreciate if anyone knows of a solution to this problem. In reality, my table has hundreds of thousands of rows, so I cannot do the calculations manually.
Is there a way to use Power Query or DAX to create a calculated column with the Raw Sales figure for each month? Something that would check if Product and Year are equal, then subtract the Jan figure from the Feb figure and so on?
Any help will be very much appreciated,
Sales Table
Product
Sales (YTD)
Month
Year
TV
1
Jan
2021
Radio
4
Jan
2021
Cooker
5
Jan
2021
TV
3
Feb
2021
Radio
5
Feb
2021
Cooker
6
Feb
2021
TV
3
Mar
2021
Radio
6
Mar
2021
Cooker
8
Mar
2021
TV
5
Apr
2021
Radio
7
Apr
2021
Cooker
8
Apr
2021
TV
7
May
2021
Radio
8
May
2021
Cooker
8
May
2021
TV
9
Jun
2021
Radio
10
Jun
2021
Cooker
10
Jun
2021
TV
10
Jul
2021
Radio
10
Jul
2021
Cooker
10
Jul
2021
TV
11
Aug
2021
Radio
13
Aug
2021
Cooker
12
Aug
2021
TV
11
Sep
2021
Radio
13
Sep
2021
Cooker
12
Sep
2021
TV
12
Oct
2021
Radio
14
Oct
2021
Cooker
13
Oct
2021
TV
17
Nov
2021
Radio
19
Nov
2021
Cooker
17
Nov
2021
TV
19
Dec
2021
Radio
20
Dec
2021
Cooker
20
Dec
2021
TV
4
Jan
2022
Radio
2
Jan
2022
Cooker
3
Jan
2022
TV
5
Feb
2022
Radio
3
Feb
2022
Cooker
5
Feb
2022
Thanks, Jim
Give this a try in powerquery / M. It groups on Product and Year, then sorts the months, and subtracts each row from the next row to determine the period amount.
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"Product", "Year"}, {
{"data", each
let r=Table.Sort(Table.AddIndexColumn(_, "Index", 0, 1),{ each List.PositionOf({"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}, [Month]), {"Month",Order.Ascending}}),
x= Table.AddColumn( r, "Period Sales", each if [Index]=0 then [#"Sales (YTD)"] else [#"Sales (YTD)"]-r{[Index]-1}[#"Sales (YTD)"])
in x
, type table }
}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Sales (YTD)", "Month", "Period Sales"}, {"Sales (YTD)", "Month", "Period Sales"})
in #"Expanded data"
I'm having issues with the following calculation:
"Kunden Anzahl" refers to the number of customers via distinct count, with %SG being the percentage. The issue is that Power BI is taking the grand total of 21.801 for the calculation of the percentages instead of the corresponding total of each month (19.337 for 12/2020, 21.391 for 12/2021). This results in the total percentage being less than 100% except for 01/2022.
So what it should do is divide each customer count by the total of its corresponding month - for example 6.326 / 19.337 = 32.7%
I've tried grouping the denominator by month and ignoring the categories by using REMOVEFILTERS but this just replicates the first column.
Any help is greatly appreciated!
Reference to comment:
Data model looks like this - JJJJMM is connected to a date table:
Assuming your data looks like this
CustomerID
Category
Month
14
A
01 January 2021
21
A
01 January 2021
29
B
01 January 2021
39
B
01 January 2021
6
B
01 January 2021
18
C
01 January 2021
34
A
01 February 2021
29
A
01 February 2021
4
A
01 February 2021
17
B
01 February 2021
24
B
01 February 2021
39
B
01 February 2021
11
B
01 February 2021
1
B
01 February 2021
42
A
01 March 2021
46
A
01 March 2021
2
A
01 March 2021
30
B
01 March 2021
DAX Calculation
CountDistinct (%) =
VAR CurrentValue =
DISTINCTCOUNT ( 'Table'[CustomerID] )
VAR AllMonth =
CALCULATE (
DISTINCTCOUNT ( 'Table'[CustomerID] ),
ALL ( 'Table' ),
VALUES ( 'Table'[Month] )
)
RETURN
DIVIDE ( CurrentValue, AllMonth )
Output
Reference
https://www.sqlbi.com/articles/using-allexcept-versus-all-and-values/
I am struggling with a requirement to display quarterly data.
I created a disconnected table to filter quarters as below with index column using
DAX: Index = RANKX('Parameter Table','Parameter Table'[Year Quarter],,ASC,Dense)
My requirement is as below:
When I select 2021Q4 it should display previous all years Q4 data including 2021Q4.
Ex:
2021Q4
2020Q4
2019Q4
2018Q4
When I select 2021Q3 it should display 2021Q3 and previous years Q4 data. same logic for Q2 or Q1 as well.
Ex:
2021Q3
2020Q4
2019Q4
2018Q4
Sample date:
Year Quarter Country Value
2018 Q4 America 10
2018 Q4 Australia 15
2018 Q4 UK 20
2019 Q1 America 25
2019 Q2 Australia 30
2019 Q3 UK 35
2019 Q4 America 40
2019 Q1 America 45
2019 Q2 Australia 50
2019 Q3 America 55
2019 Q4 UK 60
2020 Q1 America 65
2020 Q2 America 70
2020 Q3 Australia 75
2020 Q4 America 80
2021 Q1 UK 85
2021 Q2 America 90
2021 Q3 America 95
2021 Q4 Australia 100
2022 Q1 UK 105
2022 Q1 America 110
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
There are many method of having a measure to show percentage in a column of table ,
but cannot find a method to always show the ratio of a SPECIFIC group in percentage between two category.
data sample:
YEAR MONTH TYPE AMOUNT
2020 Jan A 100
2020 Feb A 250
2020 Mar A 230
2020 Jan B 158
2020 Feb B 23
2020 Mar B 46
2019 Jan A 499
2019 Feb A 65
2019 Mar A 289
2019 Jan B 465
2019 Feb B 49
2019 Mar B 446
2018 Jan A 13
2018 Feb A 97
2018 Mar A 26
2018 Jan B 216
2018 Feb B 264
2018 Mar B 29
2018 Jan A 314
2018 Feb A 659
2018 Mar A 226
2018 Jan B 469
2018 Feb B 564
2018 Mar B 164
My Goal is always show the percentage of A compare with the total amount
YEAR and MONTH are used to synchronize with slicer.
e.g. I select YEAR = 2020 , MONTH = Jan
100/258 = 38%
Manually inputted in textbox
First, Create these following 3 measures in your table-
1.
amount_A =
CALCULATE(
SUM(pie_chart_data[AMOUNT]),
FILTER(
ALLSELECTED(pie_chart_data),
pie_chart_data[TYPE] = "A"
)
)
2.
amount_overall =
CALCULATE(
SUM(pie_chart_data[AMOUNT]),
ALLSELECTED(pie_chart_data)
)
3.
amount_A_percentage = [amount_A]/[amount_overall]
Now, add both measure amount_A and amount_overall to your donut chart's values column. And place the amount_A_percentage measure to a Card and place the card in center of the Donut chart. The presentation will be as below finally-