Power Pivot - calculating distinctcount per week (rather than per day) - powerbi

I am having problems with a distinctcount calculated by week. I have the pivot table below. I want to calculate the distinct number of vendors that have sold more than $2400 per week.
I have the following data table "sales" (only the first rows, but it has several vendors and other weeks as well):
sales day sales week vendor ID Total Sales
02.11.2020 45 vendor 1 405
03.11.2020 45 vendor 1 464
04.11.2020 45 vendor 1 466
05.11.2020 45 vendor 1 358
06.11.2020 45 vendor 1 420
07.11.2020 45 vendor 1 343
I have tried to calculate it as such:
= [vendor] =distinctcount('Sales'[vendor ID])
= [Total_sales] = sum('Sales'[Total Sales])
= [# vendors - 2400] =calculate([vendor],filter('Sales',[Total_sales]>2400))
I know that this calculation considers the sales per day, not per week. so, if instead of using $2400 I used $300, for instance, then both vendors would be marked, since in at least one day, the sales of both are higher than $300. But I only want to consider the sales in a weekly basis.
What I expect (check pivot table below): Vendor 2 would be marked (sales = 2456), but not vendor 1 (sales = 1341), i.e., total number of vendors = 1. However, none of the vendors are being counted, since no daily sales are higher then $2400
Row Labels # Vendors (distinct) total sales
Store A 3797
week 45 3797
Vendor 1 1341
02.11.2020 348
04.11.2020 202
05.11.2020 335
06.11.2020 308
07.11.2020 148
Vendor 2 2456
02.11.2020 405
03.11.2020 464
04.11.2020 466
05.11.2020 358
06.11.2020 420
07.11.2020 343
I also tried to create a column of sales in which I removed the day filter, like this:
=calculate([total_sales],ALL('sales'[sales day]))
and then recalculated the [# vendors - 2400], but it still gets me the same result as above.
The question is: how do I get to consider the total sales value per week (and not per day) for the distinctcount. Thank you for the help!

Do you have a Date calendar in your file? if no try to make one, then have a relationship from date to sales day (assuming this has your dates). That way you should be able to summarize by any date grouping eg, Month, Day, Week, Quarter etc...Or you can try parsing the other date field and add new columns to your table = weeknum(Tablename[sales day])

Related

DAX formula providing the previous value of a variable, sorted by a different variable

I have two tables, Table1 and Table2, containing the following information:
Table1: Sales
Date Firm A Firm B Firm C
30-05-2022 100 200 300
29-05-2022
28-05-2022
27-05-2022 130 230 330
26-05-2022 140 240 340
25-05-2022 150 250 350
and
Table2: Dates
Relative day Date
1 30-05-2022
2 27-05-2022
3 26-05-2022
4 25-05-2022
In my Power BI (PBI) desktop a slicer, allowing the user to select from a range of Relative days (i.e. number of business days from today's date), is present.
What I want is to create a new measure, Sales lag, that contains the lagged value of sales for the individual firm, and for which the lagged value is based on the Relative day variable e.g.:
For the slicer set according to Relative day=1
Sales Sales lag
Firm A 100 130
Firm B 200 230
Firm C 300 330
Please note that I (think I) need the measure to be based on the relative day variable, as the Date variable does not take into account business days.
I previously used a measure that I think was similar to:
Sales lag =calculate(sum(Table1[Sales],dateadd('Table2'[Date],-1,day))
While this measure provided the correct results most of the time, it did not in the presence of weekends.
Thank you
I used some sample data structured slightly differently than you provide and took a stab at providing you with a solution. Find the sample data I used below and the Sales lag measure in the Solution Measure section.
Table1
Date
Firm
Sales
30-05-2022
A
100
29-05-2022
A
110
28-05-2022
A
120
27-05-2022
A
130
26-05-2022
A
140
25-05-2022
A
150
30-05-2022
B
200
29-05-2022
B
210
28-05-2022
B
220
27-05-2022
B
230
26-05-2022
B
240
25-05-2022
B
250
30-05-2022
C
300
29-05-2022
C
310
28-05-2022
C
320
27-05-2022
C
330
26-05-2022
C
340
25-05-2022
C
350
Table2
Relative day
Date
1
30-05-2022
2
27-05-2022
3
26-05-2022
4
25-05-2022
Solution Measure
Sales lag =
VAR sel = SELECTEDVALUE(Table2[Relative day])
VAR dat = CALCULATE(MAX(Table2[Date]), Table2[Relative day] = sel + 1)
RETURN
IF(
ISBLANK(sel),
"",
CALCULATE(
MAX(Table1[Sales]),
ALL(Table2[Relative day]),
Table1[Date] = dat
)
)
Sample Result

How to get a percentage of grouped values over a period of time (at the hour scale) in DAX?

I have a dataset containing the duration (in minutes) of occupancy events over a period of 1 hour in my rooms:
# room date duration
--- ---- ------------------- --------
0 A1 2022-01-01 08:00:00 30
1 A1 2022-01-01 10:00:00 5
2 A1 2022-01-01 16:00:00 30
3 A1 2022-01-02 10:00:00 60
4 A1 2022-01-02 16:00:00 60
...
My date column is linked to a date table in which I have:
# datetime year month monthName day dayOfWeek dayName hour
--- ------------------- ---- ----- --------- --- --------- -------- ----
...
k 2022-01-01 08:00:00 2022 1 January 1 5 Saturday 8
k+1 2022-01-01 09:00:00 2022 1 January 1 5 Saturday 9
...
n 2022-03-01 22:00:00 2022 3 March 1 1 Tuesday 22
I am trying to retrieve the following percentage: duration/timeperiod through a measure. The idea behind using a measure is :
Being able to use a time slicer and see my percentage being updated
Using, for example, a bar chart with my date hierarchy, and being able to see a percentage in my different level of hierarchy (datetime -> year -> month -> dayOfWeek -> hour)
Attempt
My idea was to create a first measure that would return the number of minutes between the first and the last date currently chosen. Here is what I came up with:
Diff minutes = DATEDIFF(
FIRSTDATE( 'date'[date] ),
LASTDATE( 'date'[date] ),
MINUTE
)
The idea was then to create a second measure that would divide the SUM of the durations by the Diff minutes' measure:
My rate = DIVIDE(
SUM( 'table'[duration] ),
[Diff minutes]
)
I currently face a few issues:
The slicer is set to (2022-01-02 --> 2022-01-03) and if I check in a matrix, I have datetime between 2022-01-02 0:00:00 and 2022-01-03 23:00:00, but my measure returns 1440 which is the number of minutes in a day but not in my selected time period
The percentage is also wrong unfortunately. Let's take the example that I highlighted in the capture. There are 2 values for the 10h slot, 5min and 60min. But the percentage shows 4.51% instead of 54.2%. It actually is the result of 65/1440, 1440 being the total of minutes for my whole time period, not my 10h slot.
Examples
1- Let's say I have a slicer over a period of 2 days (2022-01-01 --> 2022-01-02) and my dataset is the one provided before:
I would have a total duration of 185 minutes (30+5+30+60+60)
My time period would be 2 days = 48h = 2880 minutes
The displayed ratio would be: 6.4% (185/2880)
2- With the same slicer, a matrix with hours and percentage would give me:
hour rate
---- -----
0 0.0%
1 0.0%
...
8 25.0% <--- 30 minutes on the 1st of January and 0 minutes on the 2nd
9 0.0% <--- (5+0)/120
10 54.2% <--- (5+60)/120
...
16 75.0% <--- (30+60)/120
Constraints
The example I provided only has 1 room. In practice, there are n rooms and I would like my measure to return the percentage as the mean of all my rooms.
Would it be possible ? Have I chosen the right method ?
The DateDiff function you have created should work, I have tested it on a report and when I select some dates, it gives me the difference between the first and last selected dates.
Make sure your slicer is interacting with the measure.
In the meantime, I think I found a simpler and easier way to do it.
First, I added a new column to my date table, that seems dubious but is actually helpful:
minutes = 60
This allows me to get rid of the DATEDIFF function. My rate measure now looks like this:
My rate = DIVIDE(
SUM( table[duration] ),
[Number of minutes],
0
)
Here, I use the measure Number of minutes which is simply a SUM of the values in the minutes column. In order to provide accurate results when I have multiple rooms selected, I multiplied the number of minutes by the number of rooms:
Number of minutes = COUNTROWS( rooms ) * SUM( 'date'[minutes] )
This now works perfectly with my date hierarchy!

While dividing two measures in DAX, How can I get a SUM of the measure to display correctly? The divide function doesn't work for me

I have a DAX formula that i have used:
Price = DIVIDE([Sales Amount], [Net Sales Quantity])
My issue is that the SUM of Price is dividing the totals of Sales Amount and Net Sales Quantity instead of displaying the SUM of Price correctly.
Here's an example of what i see in Power BI:
Product Code
Sales Amount
Net Sales Quantity
Price
AIU
70
10
7
JID
36
6
6
DII
10
5
2
TOTAL
116
21
5.5
As you can see above, the total for Price is not adding up column, instead its dividing the totals for Sales Amount and Net Sales Quantity which is 116/21 = 5.5
The actual result that I need is:
Product Code
Sales Amount
Net Sales Quantity
Price
AIU
70
10
7
JID
36
6
6
DII
10
5
2
TOTAL
116
21
15
As you can see, its simply adding up the Price and SUM for the price is correct.
I'm new to power bi, i've looked at documentation and have not been able to solve this. Any help is appreciated!
You can try with Column instead of Measure

PowerBI and filtered sum calculation

I should be able to make a report concerning a relationship between sick leaves (days) and man-years. Data is on monthly level, consists of four years and looks like this (there is also own columns for year and business unit):
Month Sick leaves (days) Man-years
January 35 1,5
February 0 1,63
March 87 1,63
April 60 2,4
May 44 2,6
June 0 1,8
July 0 1,4
August 51 1,7
September 22 1,6
October 64 1,9
November 70 2,2
December 55 2
It has to be possible for the user to filter year, month, as well as business unit and get information about sick leave days during the filtered time period (and in selected business unit) compared to the total sum of man-years in the same period (and unit). Calculated from the test data above, the desired result should be 488/22.36 = 21.82
However, I have not managed to do what I want. The main problem is, that calculation takes into account only those months with nonzero sick leave days and ignores man-years of those months with zero days of sick leaves (in example data: February, June, July). I have tried several alternative functions (all, allselected, filter…), but results remain poor. So all information about a better solution will be highly appreciated.
It sounds like this has to do with the way DAX handles blanks (https://www.sqlbi.com/articles/blank-handling-in-dax/). Your context is probably filtering out the rows with blank values for "Sick-days". How to resolve this depends on how your data are structured, but you could try using variables to change your filter context or use "IF ( ISBLANK ( ... ) )" to make sure you're counting the blank rows.

Power BI - Calculate Percentage Measure based on Top N Selection

I have a table like this.
Company Amount Year
A 200 2016
B 300 2016
C 400 2016
A 500 2017
B 600 2017
C 700 2017
A 100 2016
B 400 2016
C 100 2016
A 600 2017
B 133 2017
C 50 2017
I am looking for a measure calculate the Percentage of amount that top 2 companies(based on amount) contributes to that particular year's total amount. This needs to be dynamic based on the values of Year slicer. (For Example if 2 years are selected, then the top 2 companies needs to be based on the total amount that the company has spent on those 2 years).
How about this as a measure?
PercentTop2 =
DIVIDE(
SUMX(
TOPN(2,
SUMMARIZECOLUMNS(Companies[Company],
"Amount", SUM(Companies[Amount])),
[Amount]),
[Amount]),
SUMX(ALLSELECTED(Companies), Companies[Amount]))
The TOPN(2,[...]) finds the top 2 rows of the summarized table. Then you divide the sum of those two rows by the sum of all the selected rows.