I have a problem with calculating measure that sums values for 3 previous periods.
Below I attach sample fact table and dict table to show problem I am facing.
date
customer
segment
value
01.01.2021
1
A
10
02.01.2021
1
A
10
03.01.2021
1
A
10
04.01.2021
1
A
10
01.01.2021
2
B
20
02.01.2021
2
B
30
03.01.2021
2
B
40
dict table:
segment
segment_desc
A
Name of A
B
Name of B
Approach I have taken:
last 3 value =
VAR DATES = DATESINPERIOD(facts[date],LASTDATE(facts[date]), -3,MONTH)
RETURN CALCULATE([sum value], DATES)
It produces correct results as long as there is at least one record for April.
When I use filter on segment_desc = 'B'
It produces result as I attached - so we see result in April equals 20, which is obviously not what I wanted. I would expect it to be 50.
Answer to the main question:
time intelligence functions like DATESINPERIOD require a proper calendar table, because they expect continuous dates, without gaps.
Answer to the follow-up question "why the measure shows value for January?"
It's a bit tricky. First, notice that LASTDATE in this filter context returns blank:
So, your DAX measure then becomes this:
last 3 value =
VAR DATES = DATESINPERIOD(facts[date], BLANK(), -3,MONTH)
RETURN CALCULATE([sum value], DATES)
Blank - 3 month does not make sense, so the way DAX resolves this: it replaces BLANK with the first (min) date in the table. In this case, it's 1/1/2021. Then it goes back 3 months from that date. As a result, the final measure is:
last 3 value =
CALCULATE([sum value], {2020-11-01, 2020-12-01, 2021-01-01 })
Since you have no data prior to 2021-01-01, the final result shows only January values.
Related
I have a model that's comprised of multiple tables containing, for every ID, multiple rows with a valid_from and valid_to dates.
This model has one table in that is linked to every other table (a table working as both a fact and a dimension).
This fact has bi-directional cross filtering with the other tables.
I also have a date dimension that is not linked to any other table.
I want to be able to calculate the sum of a column in this table in the following way:
If a date range is selected, I want to get the sum of the latest value per ID from the fact able that is before the max selected date from the date dimension.
If no date is selected, I want to get the sum of the current version of the value per ID.
This comes down to selecting the latest value per ID filtered on the dates.
Because of the nature of the model (bi-directional with the fact/dimension table), I want to have the latest version of any attribute from any dimension selected in the visual.
Here's an data example and the desired outcome:
fact/dimension table:
ID
Valid_from
Valid_to
Amount
SK_DIM1
SK_DIM2
1
01-01-2020
05-12-2021
50
1234
6787
1
05-13-2021
07-31-2021
100
1235
6787
1
08-01-2021
12-25-2021
100
1236
6787
1
12-26-2021
12-31-2021
200
1236
6787
1
01-01-2022
12-31-9999
200
1236
6788
Dimension 1:
ID
SK
Valid_from
Valid_to
Name
1
1234
10-20-2019
06-01-2021
Name 1
1
1235
06-02-2021
07-31-2021
Name 2
1
1236
08-01-2021
12-31-9999
Name 3
Dimension 2:
ID
SK
Valid_from
Valid_to
Name
1
6787
10-20-2019
12-31-2021
Name 1
1
6788
01-01-2022
12-31-9999
Name 2
My measure is supposed to do the following:
If no date is selected than the result will be a matrix like the following:
Dim 1 Name
Dim 2 Name
Amount Measure
Name 3
Name 2
200
If July 2021 is selected than the result will be a matrix like the following:
Dim 1 Name
Dim 2 Name
Amount Measure
Name 2
Name 1
100
So the idea here is that the measure would filter the fact table on the latest valid value in the selected date range, and then the bi-directional relationships will filter the dimensions to get the corresponding version to that row with the max validity (last valid row) in the selected range date.
I have tried to do the following two DAX codes but it's not working:
Solution 1: With this solution, filtering on other dimensions work and I get the last version in the selected date range for all attributes of all used dimensions. But the problem here is that the max valid from is not calculated per ID, so I only get the max valid from overall.
Amount Measure=
VAR _maxSelectedDate = MAX(Dates[Dates])
VAR _minSelectedDate = MIN(Dates[Dates])
VAR _maxValidFrom =
CALCULATE(
MAX(fact[valid_from]),
DATESBETWEEN(fact[valid_from], _minSelectedDate, _maxSelectedDate)
|| DATESBETWEEN(fact[valid_to], _minSelectedDate, _maxSelectedDate)
)
RETURN
CALCULATE(
SUM(fact[Amount]),
fact[valid_from] = _maxValidFrom
)
Solution 2: With this solution, I do get the right max valid from per ID and the resulting number is correct, but for some reason, when I use other attributes from the dimensions, it duplicates the amount for every version of that attribute. The bi-directional filtering does not work anymore with Solution 2.
Amount Measure=
VAR _maxSelectedDate = MAX(Dates[Dates])
VAR _minSelectedDate = MIN(Dates[Dates])
VAR _maxValidFromPerID =
SUMMARIZE(
FILTER(
fact,
DATESBETWEEN(fact[valid_from], _minSelectedDate, _maxSelectedDate)
|| DATESBETWEEN(fact[valid_to], _minSelectedDate, _maxSelectedDate)
),
fact[ID],
"maxValidFrom",
MAX(fact[valid_from])
)
RETURN
CALCULATE(
SUM(fact[Amount]),
TREATAS(
_maxValidFromPerID,
fact[ID],
fact[valid_from]
)
)
So if somebody can explain why the bi-directional filtering doesn't work anymore that will be great, and also, more importantly, if you have any solution to have both the latest value per ID and still keep filtering on other attributes, that would be great!
Sorry for the long post, but I thought it's best to give all the details for a complete understanding of my issue, this has been picking my brain since few days now and I'm sure I'm missing something stupid but I turned to this community for help because I cannot seem to be able to find a solution!
Thank you very much in advance for any help!
Seems to be workable with a dummy model. I didn't got the point how filter ID, so if it creates a problem let me know how you handle ID. Then I changed fact to facts as fact is a function. Also, I'm not sure about the workability of the measure at your real model. Hope you will give some feedback.
Amount Measure =
VAR ValidDate=
calculate(
max(facts[Valid_to])
,ALLEXCEPT(facts,facts[ID])
,facts[Valid_to]<=MAX(Dates[Date])
)
Return
CALCULATE(
SUM(facts[Amount])
,TREATAS({ValidDate},facts[Valid_to])
)
I have production figures which sum the quantity of parts produced by each workstation on a weekly basis. These are shown in a matrix with workstation number on the rows and week numbers across the columns.
I want to be able to select 2 weeks and then
Display all the weeks in the range selected by a slicer.
Show the quantity values for only the first and last selected weeks.
The first part is working using a simple Week Slicer.
The second part, however, always shows the totals for all the selected weeks rather than the 2 individual week quantities.
In this example I have selected weeks 4 and 9 and therefore the expected values for quantities are 11,505 and 49,425 as shown in the Good Qty data with red frames.
The measures to extract the fist and last selected week numbers are working:
SelWeek1 = FIRSTNONBLANK(VALUES(vLink[Week]),0)
SelWeek2 = LASTNONBLANK(VALUES(vLink[Week]),0)
My measures for the week quantities are:
IF([SelWeek1]>0,
CALCULATE([Sum vGood Qty],FILTER(vLink, vLink[Week] = [SelWeek1])),
0
)
and
SelWeek2 Qty =
IF([SelWeek2]>0,
CALCULATE([Sum vGood Qty],FILTER(vLink, vLink[Week] = [SelWeek2])),
0
)
What am I missing?
Try to use below measures:
SelWeek1 = MIN(vLink[week])
Measure =
VAR _selWeek = [SelWeek1]
VAR result =
CALCULATE(
[Sum vGood Qty],
vLink[Week] = _selWeek
)
RETURN
result
and for selected week 2 change min to max in the first measure and _selWeek variable in the second measure respectively.
I want to define a measure which returns the sum of a certain column only for the first date in the range, otherwise it should return the value 0. Idea is that I want to use this measure as a baseline in a waterfall chart which I will add to the delta values.
Example Data
Date
Backlog
1.1.2021
10
1.1.2021
12
2.1.2021
11
2.1.2021
13
3.1.2021
14
3.1.2021
15
Expected Result
Date
Backlog
1.1.2021
22
2.1.2021
0
3.1.2021
0
You can define measure like this:
Measure =
var minDate = MINX(ALLSELECTED('Table'), 'Table'[Date])
return IF(MAX('Table'[Date]) = minDate, SUM('Table'[Backlog]), 0)
Where you will get the minimum date in the current filter context and then sum up the values up to this date, or 0 for the dates after.
If you want to use the minimum date available, regardless of the current filter, replace ALLSELECTED with ALL in the code above.
I am attempting to make a measure that calculates:
SUM(Num_Compliant) / SUM(Num_Asked) for a District, filtered to the current Fiscal Year (i.e. Date on or after 07/01/2018, or July 1, 2018).
District Date Section Num_Compliant Num_Asked
A 11/12/2018 I 3 8
A 1/12/2018 I 3 8
A 11/17/2018 II 1 6
A 5/18/2018 II 3 6
B 2/20/2019 I 4 8
B 4/20/2018 I 5 8
B 11/12/2018 II 6 6
B 1/12/2018 II 1 6
C 11/17/2018 I 2 8
C 5/18/2018 I 3 8
C 4/20/2018 II 5 6
With the above sample data, I expect the following results
District Numerator Denominator Value
A 4 14 0.29
B 10 14 0.71
C 2 8 0.25
I intentionally made the denominator different in district C to reflect the fact that sections are sometimes missing.
I have been trying to solve this for about a day, and am brand-new to Power BI, so I apologize if this is a very simple question.
The following measure looks like it solves your needs:
Value =
var __date = DATE(2018,7,1)
var __Numerator = CALCULATE(sum(Compliancy[Num_Compliant]),ALLEXCEPT(Compliancy,Compliancy[Distict]),Compliancy[Date]>__date)
var __Denominator = CALCULATE(sum(Compliancy[Num_asked]),ALLEXCEPT(Compliancy,Compliancy[Distict]),Compliancy[Date]>__date)
return __Numerator/__Denominator
Please note you will have to change "Compliancy" to your table name throughout the DAX formula. Also, the date is currently hard coded into the DAX, it might be best to use a date slicer so you can change the values on the fly. The measure without the date filter hard coded as is as follows:
Value =
var __Numerator = CALCULATE(sum(Compliancy[Num_Compliant]),ALLEXCEPT(Compliancy,Compliancy[Distict]))
var __Denominator = CALCULATE(sum(Compliancy[Num_asked]),ALLEXCEPT(Compliancy,Compliancy[Distict]))
return __Numerator/__Denominator
Table in Power BI:
As a side note of things to watch out for:
The measure "Value" needs to be set to the format "Decimal Place"
with 2 decimals.
The date field must be appropriately set to the
"Date" data type.
I am working on powerbi, using data over 2 months period, writing dax queries and trying to build reports.
I am trying to show the monthly values in cards visuals.
And i am able to do it using the measure below.
Sample Data:-
PlanPrevMon = CALCULATE([PlanSum],PREVIOUSMONTH('Month Year'[Date]))
CustomKPI = IF(ISBLANK([PlanSum]),"No Data Available ",[PlanSum])&" "&IF([PlanSum]=[PlanPrevMon],"",IF([PlanSum] > [PlanPrevMon],UNICHAR(8679),UNICHAR(8681))&IF([PlanSum]<=0,"",""))
But here i don't want user to choose the month values from slicer.
I would like to show the card values as
if current month value is not available then it should compare with the previous month value automatically as shown in an image below
For example, Apr-2017 value is not available; in this scenario I would like it to compare with the Mar-2017 value.
If Mar-2017 value also not available, then the previous month value and so on
Edit
I tried what #user5226582 suggested but still getting wrong values as below image.
As you can see i restricted to crosscheck whether the value is getting in a right way of not.
But not.
This is the measure i have used as #user5226582 suggested.
c =
var temp = 'Revenue Report'[Date]
return CALCULATE(LASTNONBLANK('Revenue Report'[Planned Rev],
'Revenue Report'[Planned Rev]),
ALL('Revenue Report'),
'Revenue Report'[Date]<=temp)`
Can you please correct me if i am doing anything wrong
Does this help...?
To get the PlanPrevMon column to show like this, I first created a new index column:
id = COUNTROWS(FILTER(Sheet1, EARLIER(Sheet1[Date],1)>Sheet1[Date]))
Then I used the index to help create the PlanPrevMon column in two steps:
Step 1: I made one column named PlanPrevMon1.
PlanPrevMon1 = SUMX(FILTER(Sheet1,Sheet1[id]=EARLIER(Sheet1[id])-1),Sheet1[PlanSum])
Step 2: I made another column named PlanPrevMon.
PlanPrevMon = if(ISBLANK(Sheet1[PlanPrevMon1]),
if(Sheet1[id]=1,0,CALCULATE(LASTNONBLANK(Sheet1[PlanPrevMon1],Sheet1[PlanPrevMon1]),ALL(Sheet1),ISBLANK(Sheet1[PlanSum]))),
Sheet1[PlanPrevMon1])
For the card, I used this measure:
Card = CALCULATE(LASTNONBLANK(Sheet1[PlanPrevMon],1),FILTER(Sheet1,Sheet1[id]=max(Sheet1[id])))
I hope this helps.
You can use LASTNONBLANK DAX function.
Example data:
a b
1 1
2
3 2
4
5 3
Calculated column:
c =
var temp = Table1[a]
return CALCULATE(LASTNONBLANK(Table1[b],Table1[b]),ALL(Table1),Table1[a]<=temp)
Results in:
a b c
1 1 1
2 1
3 2 2
4 2
5 3 3