I'm newish to Power BI/DAX, and I'm having trouble getting a running total to work the way I need. Assume the following table for data:
User month sales
UserA 1/1/2019 1
UserB 1/1/2019 3
UserC 1/1/2019 2
UserA 2/1/2019 1
UserB 2/1/2019 3
UserC 2/1/2019 2
UserA 3/1/2019 1
UserB 3/1/2019 3
UserC 3/1/2019 2
I've been looking around and I've found the following formula gives me a good running total the way I need:
AllSales =
calculate(
sum('table'[Sales]),
filter(
all ('table'),
'table'[date] <= max ('table'[date])
)
)
--
Total 6 12 18 18
The problem comes when I want to see this in matrix form with the users breaking out into buckets. When I do this, the number of sales is the same for each user:
UserA 6 12 18 18
UserB 6 12 18 18
UserC 6 12 18 18
Total 6 12 18 18
My desired outcome would look like this:
UserA 1 2 3 3
UserB 3 6 9 9
UserC 2 4 6 6
Total 6 12 18 18
I believe I understand why the ALL function is causing this, but I don't know how to tweak it or which function to switch to in order to resolve this issue. Any help would be very much appreciated. Thanks!
Instead of applying ALL to the entire table, apply it only to the column you need:
AllSales =
CALCULATE (
SUM ( 'table'[Sales] ),
FILTER ( ALL ( 'table'[date] ), 'table'[date] <= MAX ( 'table'[date] ) )
)
Related
I would like to first state that I am a beginner with DAX and this is one of my attempts (which seemed to be the closest to the solution I need). I come from a SQL heavy background so my "thinking" is somehow fixed in that way.
I have tried to solve this by implementing something that would match the following SQL logic:
CASE WHEN MAX(column) OVER (PARTITION BY group) = column2 THEN column3 ELSE "" END
However, this doesn't seem to work directly like in SQL, so I would like to ask for some help.
I have the current set of data, which is imported from a simple text file.
ID GroupID Amount
1 2 8502
2 2 8502
3 2 8502
4 2 8502
1 6 80
2 6 80
And I would like to find a way to get the following result:
ID GroupID Amount LatestGroupAmount
1 2 8502
2 2 8502
3 2 8502
4 2 8502 8502
1 6 80
2 6 80 80
And then have a Total under LatestGroupAmount, totaling to 8582.
So far, I have created 2 new measures in my table, MaxID and MaxIDbyGroup.
MaxID = MAX(data[ID])
and
MaxIDbyGroup = CALCULATE([MaxID], ALLEXCEPT(data, data[GroupID]))
This gives me:
ID GroupID Amount MaxID MaxIDbyGroup
1 2 8502 1 4
2 2 8502 2 4
3 2 8502 3 4
4 2 8502 4 4
1 6 80 1 2
2 6 80 2 2
Now, I would like to create a new measure that just does a lookup of the Amount, based on the equality between ID and MaxIDbyGroup.
I have tried to create a new measure with the following definition:
LatestGroupAmount = LOOKUPVALUE(data[Amount], data[GroupId], data[MaxIDbyGroup])
But this gives me the following output:
ID GroupID Amount LatestGroupAmount
1 2 8502
2 2 8502
3 2 8502
4 2 8502
1 6 80 8502
2 6 80 8502
Edit:
I have created another measure:
MaxGrid = MAX(data[GroupID])
And I have tried using CALCULATE with the following definition for LatestGroupAmount:
LatestGroupAmount = CALCULATE(
SUM( data[Amount] ),
FILTER( data, data[ID] = data[MAXID_by_author]), FILTER(data, data[GroupID] = data[MaxGrid]) )
And it seems to show what I want, however, it filters the 6 rows I have to only 2 rows (although I think it does an aggregation).
ID GroupID Amount LatestGroupAmount
4 2 8502 8502
2 6 80 80
The reason I say I think it's an aggregation, is because I add the MaxID to the widget, the output shows the correct number of rows. Essentially, the image below is the output that I want, except for the MaxID column.
If I remove the MaxID column, the widget automatically summarizes to two rows, but I want to show all of the 6 rows.
You can use this measure to achieve your result:
LatestGroupAmount =
VAR TT01 = ADDCOLUMNS(
SUMMARIZE(Data,Data[ID],Data[GroupID]),
"MaxID",CALCULATE(MAX(Data[ID]),ALLEXCEPT(Data,Data[GroupID]))
)
RETURN
CALCULATE(MAX(Data[Amount]),
FILTER(TT01,
Data[ID] =[MaxID]))
Then define your visual table by putting [ID], [GroupID],[Amount] on rows, and above measure into values, Then:
Please Make sure that For [ID] and [GroupID Columns], show items with no data is ticked or checked, like in the picture below.
Your current definition for LatestGroupAmount is searching in the GroupId column, though I believe that should be the ID column, i.e.:
LOOKUPVALUE( data[Amount], data[ID], data[MaxIDbyGroup] )
In any case, this will fail since that column contains duplicate entries. As such, you should use something like:
LatestGroupAmount :=
CALCULATE(
MAX( data[Amount] ),
FILTER( data, data[Id] = data[MaxIDbyGroup] )
)
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]))
I have two tables.
A campagin table:
Campaign ID
Start Date
End Date
Daily Target
1
21/12/2020
15/02/2021
5
2
18/10/2020
18/01/2021
3
3
01/07/2020
03/01/2021
8
4
09/01/2021
15/05/2021
1
5
05/08/2020
09/01/2021
2
And a simple Date table:
Date
01/01/2021
02/01/2021
03/01/2021
04/01/2021
05/01/2021
06/01/2021
07/01/2021
08/01/2021
09/01/2021
10/01/2021
11/01/2021
12/01/2021
13/01/2021
What I would like to do is add a calculated column to the Date table that will calculate the sum of all the Daily Targets for campaigns that are between Start Date and End Date. So for 1st January 2021 I want to take the sum of the Daily Targets for Campaign 1, 2, 3 & 5. E.g:
Date
Total Daily Target
01/01/2021
18
02/01/2021
18
03/01/2021
18
04/01/2021
10
05/01/2021
10
06/01/2021
10
07/01/2021
10
08/01/2021
10
09/01/2021
9
10/01/2021
9
11/01/2021
9
12/01/2021
9
13/01/2021
9
I'm quite new to DAX and have tried multiple different variations of SUM(), SUMX() & FILTER() within CALCULATE(), all to no avail. I also don't know what the relationship between the two tables should be seeing as there are two dates in the Campaign table? Any help at all would be greatly appreciated.
Try this below Measure-
Measure =
var current_row_date = MIN('date'[Date])
RETURN
CALCULATE(
SUM(campaign[Daily Target]),
campaign[Start Date] <= current_row_date
&& campaign[End Date] >= current_row_date
)
output-
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
)
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.