I need to create a measure that do a count and i need that to work until 03/31/currentyear.
I have a table with a comum calendar, and I tried to do the following parameter:
'dCalendario'[Date] <= DATE(YEAR(TODAY()), 03, 31)
I'm noob in Dax and I think that would never work, but i have to try.
This can be achieved by using filter example as follows
row_count =
VAR datelimit = DATE(YEAR(TODAY()), 03, 31)
CALCULATE (
<expression> or <Measure>,
'dCalendario'[Date] <= datelimit
)
Related
I have a dataset in PowerBI that uses many specific date values. I would like to create a new column that gives the count of the date value in its specific row, throughout the dataset. For example, the first few rows might look like this:
Date
Count
June 29, 2022
2
July 8, 2006
1
June 29, 2022
2
December 15, 2019
1
And I am trying to write code for the Count column.
I have already tried using the COUNT and COUNTROWS functions as well as an adaption of the following code from this question (all to no avail):
CountOfScheduled = calculate(countrows('YourTable'), FILTER(ALL('YourTable'[Department],'YourTable'[Call Status]), 'YourTable'[Call Status] = "Scheduled" && 'YourTable'[Department] = SELECTEDVALUE('YourTable'[Department]) ))
This function might work fine if you only have a few values and can type each one into the formula, but I have many values. I don't want to type in every single date in the formula.
If you just need a calculated column
Column =
CALCULATE ( COUNT ( 'fact'[Date] ), ALLEXCEPT ( 'fact', 'fact'[Date] ) )
ALLEXCEPT is a means of defining partition, fact[date] in this case.
If you meant a measure
Measure = CALCULATE(COUNT('fact'[Date]),ALL('fact'),VALUES('fact'[Date]))
You can use EARLIER (or EARLIEST) to refer to the matching item in the same row.
So:
Count = countrows(filter('Table (3)','Table (3)'[Date]=EARLIER([Date])))
I am essentially trying to say from 1/1/2022 through 3/31/2022 use a certain measure and if not in that timeframe then use another measure. The purpose is for these months a different calculation was used to determine the total and for the remainder of the time it was the unchanged, so I want to see the total trended out over time that will not be filtered and for those 3 months show the 'Diff_Total'. I've tried a few things other than this and in this particular attempt it won't let me select a column from the 'Date_Table'...
= CALCULATE(IF(Date_Table > DATE(2022, 01, 01) && Date_Table < DATE(2022, 03, 31), [Diff_Total], [Original_Total]))
I've got it working with
Measure =
VAR Dateinrange =
AND(
min(Date_Table[Date]) > DATE(2022, 01, 01),
min(Date_Table[Date]) < DATE(2022, 03, 01)
)
RETURN IF(Dateinrange=TRUE(),[Diff_Total], [Original_Total])
Let me know if that works for you!
Working in Power BI, I've created a couple of nested measures used to calculate month over month growth.
MoM $ Adj CM-1 = CALCULATE ([MoM $],
ALL ('Calendar'),
'Calendar'[Relative Month] = "CM-1",
'Group'[Flag] = "Yes",
'Type'[Type] = "N/A"
)
This works as expected. I am trying to create another measure based on the MoM $ Adj CM-1 measure to calculate the sum of only the negative values so I can see the top declining accounts.
Top Decliners = = CALCULATE ([MoM $],
ALL ('Calendar'),
'Calendar'[Relative Month] = "CM-1",
'Group'[Flag] = "Yes",
'Type'[Type] = "N/A",
[MoM $] < 0
)
This doesn't work (throws an error).
Question is it possible to use the existing measure with an additional filter to sum only the negative values so I can keep all existing filters? Or do I need to write the measure from the base table and reapply all the filters?
It is really hard to work on powerbi measures in stack overflow because we can't reproduce your work.
However, I believe you are trying to reference a measure in calculate which normally isn't allowed with out a filter wrapper.
Rather than trying to incorporate filter in your answer, may I suggest you create a new "root" measure of negative only MoM measure and then reference that measure in the same way as your first formula?
If you want to use filter then try the below. Depending on the base measure is calculated, you need to validate if the results are correct:
Top Decliners = CALCULATE ([MoM $],filter(
ALL ('Calendar'),
'Calendar'[Relative Month] = "CM-1",
'Group'[Flag] = "Yes",
'Type'[Type] = "N/A",
[MoM $] < 0)
)
I need to calculate headcount while keeping the measure sliceable by any dimension connected to the fact table. Given the nature of my tables and model, what I need to do is a point in time calculation on a Slowly Changing Dimension Type 2.
I managed to make it work using the function KEEPFITLERS, but I need a more scalable function that wouldn't require me to list all the dimensions I want to slice by.
Here is my PowerBI file with sample data: https://gofile.io/d/smS2Hr
Here is a simplified sketch (image) of my model: https://ibb.co/fQYpsdx
Background:
The first measure I am calculating is the number of Employees at the Start of the Period (Employees SoP). If the end-user selects in PowerBI the whole month of January 2020, the Start of the Period is Jan 1st, 2020. Hence, "Employees SoP" for the month of Jan 2020 will give the number of employees on Jan 1st, 2020.
The formula below calculates the correct values for Employees SoP:
Employees SoP =
VAR MinDate = MIN ( 'Date'[Date]) //Mininum date selected by end-user in PowerBI
VAR Result =
CALCULATE (
DISTINCTCOUNT(Fact[EmployeeId]),
FILTER(ALL('Fact'), 'Fact'[EffectiveStartDate] <= MinDate
&& IF(ISBLANK('Fact'[EffectiveEndDate]), date(2050,1,1), Fact[EffectiveEndDate]) > MinDate
))
RETURN
Result
The problem with the formula above is that, because of the ALL function, the measure is not sliceable by any dimension, i.e., Pay Class and Employment Status (the same number repeats itself).
Results:
Hence, I created this other measure using KEEPFILTERS, and it works perfectly.
Employees SoP KEEPFITLERS =
VAR MinDate = MIN ( 'Date'[Date]) //Mininum date selected by end-user in PowerBI
VAR Result =
CALCULATE (
DISTINCTCOUNT(Fact[EmployeeId]),
FILTER(ALL('Fact'), 'Fact'[EffectiveStartDate] <= MinDate
&& IF(ISBLANK('Fact'[EffectiveEndDate]), date(2050,1,1), Fact[EffectiveEndDate]) > MinDate
), KEEPFILTERS(PayClass), KEEPFILTERS(EmploymentStatus))
RETURN
Result
The problem with this formula is that I have to list all the dimensions I want to slice by ( e.g., PayClass, EmploymentStatus) inside the DAX formula. This is not very scalable.
I did some experimenting with REMOVEFILTERS but it looks like it does not work with DirectQuery for now, so it wouldn't solve my production problem. Link:
https://learn.microsoft.com/en-us/dax/removefilters-function-dax
QUESTION:
How can I write this measure using an alternative to KEEPFILTERS with which I wouldn't have to list each dimension I want to slice by?
Thank you!
I just managed to solve my problem. I made both relationships to the Dates table "inactive". With that, I could remove the "ALL" function in the DAX formula and now it not only calculates the correct values, but it also slices nicely. I will have to use the USERELATIONSHIP function to calculate more complex measures but, in most of the cases, this simple solution works like a charm.
Employees SoP without ALL =
VAR MinDate = MIN ( 'Date'[Date]) //Mininum date selected by end-user in PowerBI
VAR Result =
CALCULATE (
DISTINCTCOUNT(Fact[EmployeeId]),
FILTER('Fact', 'Fact'[EffectiveStartDate] <= MinDate
&& IF(ISBLANK('Fact'[EffectiveEndDate]), date(2050,1,1), Fact[EffectiveEndDate]) > MinDate
))
RETURN
Result
I am a complete newbie to Power BI
I am trying to reproduce this type of calculation in the Power Query Editor of Power BI.
The screenshot is from Excel and in column C it uses AVERAGE($B$2: B2) and the next row is AVERAGE($B$2: B3) and so on.
C2 = ((168 + 313) / 2) = 241
C3 = (((168 + 313) + 301) / 3) = 261
and so on
Is there a way to do this type of calculation in Power Query?
In the Power Query M language, you can do a similar thing. Filter the rows up to and including the current row's date and then average the CallsPresent column.
= List.Average(
Table.SelectRows(
#"[Previous Step Name Here]",
(C) => C[FullDate] <= [FullDate]
)[CallsPresent]
)
The #"[Previous Step Name Here]" bit is simply the table that you are doing the selection on. A query in the query editor is generally a list of steps where you do one transformation at a time. So your new step is creating a custom column based on the previous step.
The (C) => syntax is a bit more tricky, but basically, it's used to allow me to compare the FullDate in the table we're operating on (#"[Previous Step Name Here]") with the FullDate in the current step. Check out this blog post for much more info related to this.
You can use an average function on the dates up to the current one.
As a calculated column, it would look something like this:
AvgPresented =
CALCULATE(
AVERAGE(
Table1[CallsPresent]),
ALL(Table1),
Table1[FullDate] <= EARLIER(Table1[FullDate]
)
)
or this:
AvgPresented:
AVERAGEX(
FILTER(
Table1,
Table1[FullDate] <= EARLIER(Table1[FullDate])
),
Table1[CallsPresent]
)
Note: The EARLIER function is referring to the earlier row context (i.e. the values in the current row) and has nothing to do with times or dates.