Dynamically get count of customers by year - powerbi

I have data like below
and would like to show it in a Power BI visual like this.
I am aware of creating pivoting and unpivoting the data, But I am looking of the count of customers in few buckets (lets say gap of 5 years) and it should be dynamic (I know this can be achieved using M language, but I am not able to wrap my head around it) Please could someone provide insights about this.

Create a calculated colum:
5 Year Bin =
FLOOR ( DIVIDE ( data[Year], 5 ), 1 ) * 5 & "-"
& FLOOR ( DIVIDE ( data[Year], 5 ), 1 ) * 5 + 4
This looks like:
Year
5 Year Bin
1910
1910-1914
1911
1910-1914
1912
1910-1914
1913
1910-1914
1915
1915-1919
1916
1915-1919
1917
1915-1919
1918
1915-1919
1919
1915-1919
1920
1920-1924
1921
1920-1924
1922
1920-1924
1923
1920-1924
1924
1920-1924
1925
1925-1929
1926
1925-1929
1927
1925-1929
1928
1925-1929
1929
1925-1929
1930
1930-1934
1931
1930-1934
To get the "5 Years Bin" to the columns add a matrix visualisation and configure it to show "5 Years Bin" in the columns and CountOfCustomers as values:
The result looks like this:
1910-1914
1915-1919
1920-1924
1925-1929
1930-1934
Total
8
10
16
41
23
98

You can use binned groups to achieve this behaviour:
In Power BI, right click on Year column and click "New group":
Then configure your binned group:
To get the Years to the columns add a matrix visualisation and configure it to show "Year (bins)" in the columns and CountOfCustomers as values:
The result looks like this:
1910
1915
1920
1925
1930
Total
8
10
16
41
23
98

Related

How to create a monthly summary table in Power BI

In PowerBI, I have a table with data in days
Table 1
Day
Order
1/1/2022
3
1/31/2022
5
2/2/2022
7
2/11/2022
12
3/1/2022
31
4/31/2022
5
4/2/2022
7
6/11/2022
21
And I want to have a summary table for months like
Table 1
Month
Order
1 2022
8
2 2022
19
3 2022
31
4 2022
12
6/11/2022
21
How can I do that using DAX?
Sure. Create a Calculated Table using the SUMMARIZECOLUMNS function.
Something like:
OrdersByMonth = summarizecolumns(MyTable[Month], "Orders", sum(MyTable[Orders]))

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.

How should I write multiple IF statements in DAX using Power BI Desktop?

On Power BI Desktop, I am working with multiple conditional IF statements. I have an original table with user IDs and SecondsToOrder, looking like this.
UserID SecondsToOrder
00001 2320
00002 13
00003 389
00004 95
... ...
I created a new calculated column MinutesRounded to rounddown seconds into minutes, and now my table is looking like this.
UserID SecondsToOrder MinutesRounded
00001 2320 38
00002 13 0
00003 389 12
00004 95 1
... ... ...
Now I want to create another column based on my calculated column MinutesRounded, where depending on a number I assign each user to one of the following groups: '< 1 minute' '<15 minutes' and '> 15 minutes'. The end result should look like this.
UserID SecondsToOrder MinutesRounded Lenght
00001 2320 38 > 15 minutes
00002 13 0 < 1 minute
00003 389 12 < 15 minutes
00004 95 1 < 1 minute
... ... ... ...
I am doing it using DAX by this statement.
Lenght = IF([MinutesRounded]<1,"< 1 minute",IF([MinutesRounded]<15,"<15 minutes", "> 15 minutes"))
And getting a syntax error. Seriously don't understand what is wrong here. Could you please help. The error I am getting is below:
The syntax for '"< 1 minute"' is incorrect. (DAX(IF([MinutesRounded]<1."< 1 minute",IF([MinutesRounded]<15."<15 minutes", "> 15 minutes")))).
For some reason I see dots and brackets appearning in the error which I haven't even typed. How should I fix it?
UPDATE: found the reason was some regional/keyboard setting within POWER BI, and that's why I had to use semicolons instead of commas. The code itself was correct.
I get no error using your DAX exactly as it is:
= IF([MinutesRounded]<1,"< 1 minute",IF([MinutesRounded]<15,"<15 minutes", "> 15 minutes"))
You can also use SWITCH:
=
SWITCH (
TRUE (),
[MinutesRounded] < 1, "< 1 minute",
[MinutesRounded] < 15, "<15 minutes",
"> 15 minutes"
)
Thanks

How to create a DAX measure for finding values in a previous month, not using a Date field?

I am currently trying to create a report that shows how customers behave over time, but instead of doing this by date, I am doing it by customer age (number of months since they first became a customer). So using a date field isn't really an option, considering one customer may have started in Dec 2016 and another starts in Jun 2017.
What I'm trying to find is the month-over-month change in units purchased. If I was using a date field, I know that I could use
[Previous Month Total] = CALCULATE(SUM([Total Units]), PREVIOUSMONTH([FiscalDate]))
I also thought about using EARLIER() to find out but I don't think it would work in this case, as it requires row context that I'm not sure I could create. Below is a simplified version of the table that I'll be using.
ID Date Age Units
219 6/1/2017 0 10
219 7/1/2017 1 5
219 8/1/2017 2 4
219 9/1/2017 3 12
342 12/1/2016 0 500
342 1/1/2017 1 280
342 2/1/2017 2 325
342 3/1/2017 3 200
342 4/1/2017 4 250
342 5/1/2017 5 255
How about something like this?
PrevTotal =
VAR CurrAge = SELECTEDVALUE(Table3[Age])
RETURN CALCULATE(SUM(Table3[Units]), ALL(Table3[Date]), Table3[Age] = CurrAge - 1)
The CurrAge variable gives the Age evaluated in the current filter context. You then plug that into a filter in the CALCULATE line.

PowerBI - Average and Variance Calculation with conditions

I am trying to calculate Variance and Average in PowerBI. I am running into Circular dependency errors.
This is my Data,
Month Year Item Count
1 2017 Chair 100
1 2017 Chair 200
1 2017 Chair 300
1 2017 Bench 110
1 2017 Bench 140
1 2017 Bench 150
2 2017 Chair 180
2 2017 Chair 190
2 2017 Chair 250
2 2017 Bench 270
2 2017 Bench 370
3 2017 Chair 120
3 2017 Chair 150
3 2017 Bench 180
3 2017 Bench 190
4 2017 Chair 200
4 2017 Chair 210
4 2017 Bench 220
4 2017 Bench 230
.
.
.
Average = Sum of Counts for the Previous 3 months / 3
Variance = (Average - Sum(CurrentMonth)) / Average
So, because the average won't be meaningful for the first 3 months, I wouldn't be worried about that.
Expected Output,
Month Year Item Sum(CurrentMonth) Average Variance
1
1
2
2
3
3
4 2017 Chair 410 497 0.21
4 2017 Bench x y z
Lets Say for Chair,
Sum of Current Month = 200 + 210 = 410
Average of Last 3 Months = (100 + 200 + 300 + 180 + 190 + 250 + 120 + 150 )/ 3 = 1490 / 3 = 497
Variance = (497 - 410) / 410 = 87 / 410 = 0.21
Kindly share your thoughts.
I started with this as Table1 (I added a couple months data to yours):
I loaded it into Power BI and added a column called "YearMonth" using this code: YearMonth = Table1[Year]&FORMAT(Table1[Month],"00") ...to get this:
Then I added another column called "Sum(CurrentMonth)" using this code: Sum(CurrentMonth) = SUMX(FILTER(FILTER(Table1,Table1[Item]=EARLIER(Table1[Item])),VALUE(Table1[YearMonth])=VALUE(EARLIER(Table1[YearMonth]))),Table1[Count]) ...to get this:
Then I added another column called "Average" using this code: Average = SUMX(FILTER(FILTER(FILTER(Table1,Table1[Item]=EARLIER(Table1[Item])),VALUE(Table1[YearMonth])<=VALUE(EARLIER(Table1[YearMonth]))-1),VALUE(Table1[YearMonth])>=VALUE(EARLIER(Table1[YearMonth]))-3),Table1[Count])/3 ...to get this:
Lastly, I added a column called "Variance" using this code: Variance = (Table1[Average]-Table1[Sum(CurrentMonth)])/Table1[Sum(CurrentMonth)] ...to get this:
I hope this helps you.