I am newbie in Power Bi. I am calculating months to date of a measure.
I have written following DAX formula for that,
MTD in Sales = CALCULATE([Total Sales], DATESMTD(Dates[Date]) )
it shows me correct total sales value of this month.But when i make day-wise, it shows me some unrealistic value.
I have attached a screenshots of my result..Please have a look.
I don't understand what wrong are going on? Can you please find out the problem plz?
DATESMTD(Dates[Date]) is equivalent to:
CALCULATETABLE(
FILTER(
ALL(Dates[Date]),
AND(
Dates[Date] <= MAX(Dates[Date]),
AND (
YEAR(Dates[Date]) = YEAR(MAX(Dates[Date])),
MONTH(Dates[Date]) = MONTH(MAX(Dates[Date]))
)
)
)
)
This only considers the maximum value of the dates in the external filter context, so for Tuesday (today), it will contain every day of the month up to today, for Monday (yesterday) it will contain every day of the month up to yesterday and so on. (Assuming that no Sales are linked to future dates).
If you want to further filter this to only include sales that occurred on the given day of the week, I'd suggest altering MTD in Sales to:
[MTD in Sales] := CALCULATE([Total Sales], DATESMTD(Dates[Date]), Dates[DayOfWeekName])
This will additionally filter the included dates to only those with DayOfWeekName values present in the external filter context.
Related
In power Bi, I am creating a report that compares this years data to last years. For example, I have January data for 2022 but I need to calculate all of the data for January in 2021. In the pic, I have CY (current year) data for January. The second matrix would display the Previous year data. the data in the PY pic is not correct. Can someone help me figure out a dax formula for it?
So the answer is going to depend on whether you have a date table in your model or not. But essentially, you need to change the filter context for the previous/last year measure using the CALCULATE function.
As an aside, I noticed your column names between the pictures were not really similar, i.e. Sales Order seems different than Actual Shipping. But in any case...
With a date table
PY Calculation = CALCULATE(
SUM( YourTable[YourColumn] ), // or some other calculation...
SAMEPERIODLASTYEAR( DateTable[Date] )
)
Without a date table
PY Calculation = CALCULATE(
SUM( YourTable[YourColumn] ), // or some other calculation...
SAMEPERIODLASTYEAR( YourTable[SomeDateColumn].[Date] )
)
Let me know if that worked for you. (And yes, there are other ways to accomplish the same thing.)
I use as a filter date between.
I would like to get the year of the end date. I try as measure yearmax=year(max(date)) but it returns wrong values. I need to extract only the year of the end date. For example, when the user select as date range 01/03/2020 till 04/04/2027, I need to get only the year of the end date and have 2027.
I put the pbix file here, any help would be appreciated.
https://drive.google.com/file/d/1AqrdiHBQdYjrQImP9AGO_sC7Duh9Yeb5/view?usp=drivesdk
This looks like a Power BI slicer visual bug.
You set filter into the slicer to restrict the selection over the last 90 days including TODAY. When selecting any day up to yesterday, your YEAR(MAX('Date'[Date])) measure works fine. But when you push the slicer up to today, then even if the slicer shows the correct date of today in the upper bound, the measure returns the last year in the date table, like when no filter exists. To fix that you might add a filter to your measure to limit the upper bound to TODAY() like follows
Year end date =
VAR MaxDate =
CALCULATE ( MAX ( 'Date'[Date] ), KEEPFILTERS ( 'Date'[Date] <= TODAY () ) )
RETURN
YEAR ( MaxDate )
Of course, maybe that the problem was just the existence of the filter in the slicer, and by removing it the original measure works ad desired
I need your help.
I have a table (“Table”) like this.
In the table below I have SUM “sales” by the LATEST 2 days with sale (not the latest 2 DATES! i.e. example: if the latest sales update is on a Tuesday, it sums the sale for Monday and Friday (no sale in weekend)) for each products.
in other words:
The calculation is made with the following DAX calculated column:
Sale last 2 days=
VAR ProductDates =
CALCULATETABLE (
VALUES ( Table[Date]),
ALLEXCEPT ( Table, Table[Product_ID])
)
VAR LastTwoDates = TOPN ( 2; ProductDates;[Date] )
RETURN
CALCULATE (
SUM ([Sale]);
ALLEXCEPT ( Table, Table[Product_ID] );
Table[Date] IN LastTwoDates)
Now, I need to take it a step further:
What I want to do is to make a new calculations which SUM the sale for each product for the latest 2 days, but ONLY for the Distributors, where the "Distributor indicator"=1. And the latest 2 sales days in question, are the sales days where there has been sale to these distributors only.
(example: if the latest sales day is a tuesday and there were no sale from these distributors yesterday, the the latest two days will be previous friday and thursday (i.e. the latest 2 days where sales is not null).
I know I can use the calculation, I have already made, but I can’t figure out where to put the logic in, in order to get the right result:
Example:
I know I can use the calculation, I have already made, but I can’t figure out where to put the logic in, in order to get the right result:
Can some of you please help!
Thanks. It is greatly appreciated.
Br,
Jakob
Assuming there is a date dimension table named "Calendar", this worked for me.
=IF(
COUNTROWS(
INTERSECT(
VALUES('Calendar'[Date]),
CALCULATETABLE(VALUES(Table[Date]), ALL(Table))
)
) > 0,
CALCULATE(
SUM(Table[Sale]),
CALCULATETABLE(
TOPN(2, SUMMARIZE(Table, Table[Date]), Table[Date], DESC),
FILTER(ALL(Table), Table[Date] <= MAX('Calendar'[Date]))
),
ALL('Calendar')
)
)
I need to show how many active contracts we have open for each month in the last 6 months. I am trying to figure out a way to display this. Here is my table
Machine Enrollment# StartDate EndDate
A 1 1/2/2016 6/18/2019
B 2 12/15/2012 5/12/2034
C 3 3/25/2019 4/25/2021
D 4 1/7/2000 7/15/2019
A 5 10/1/2019 10/1/2025
I have thousands of rows. I want to be able to show a rolling 6 month visual for how many machines are under contract. So in this small example it would look like this
Apr-19 June-19 Jul-19 Aug-19 Sep-19 Oct-19
4 4 3 2 2 3
Where do I even begin in creating this? In the past, we have just looked at the numbers for the current month and tacked those results onto the end of a static table and deleted the column from over 6 months ago. I have been assigned to automate this report in Power BI. I am guessing I need to create a column/measure that looks at the EndDate and compares it to the filtered Date in the visual (ie: Aug-19) and determines if the contract was open at that time. But I do not know. Any help is much appreciated. Thanks in advance!
I think I found a solution for what you are looking for. You may find a sample pbix file here.
1. Create a calendar table
A calendar table is required to filter/slice the time periods. The calendar table needs to have a unique Date column, and optional columns such as Year, Quarter, and Month, depending on what units of period you need in the analysis.
A calendar table can be most easily created as a DAX calculated table. Here is an example of a minimal calendar table required in this use case.
Calendar =
ADDCOLUMNS(
CALENDAR( MIN( Contracts[StartDate] ), MAX( Contracts[EndDate] ) ),
"Year Month", FORMAT( [Date], "mmm-yy" ),
"Year Month Number", YEAR( [Date] ) * 100 + MONTH( [Date] )
)
2. Create a measure to calculate number of open contracts
Every numbers calculated and shown in reports need to be defined as measures.
Let's think about the number of May-19. The current filter context includes all 31 dates in the Calendar table between 2019-05-01 and 2019-05-31. In this case, how can we think of an open contract? If the contract starts after 2019-05-31, it is not open. If the contract ends before 2019-05-01, it is not open as well. Therefore the open contract meets this condition.
Starts on or before 2019-05-31 and
Ends on or after 2019-05-01
Below is the measure definition to count the number of contracts based on this condition.
# Open Contracts =
VAR MinDate = MIN( 'Calendar'[Date] )
VAR MaxDate = MAX( 'Calendar'[Date] )
RETURN COUNTROWS(
FILTER(
Contracts,
Contracts[StartDate] <= MaxDate
&& Contracts[EndDate] >= MinDate
)
)
3. Add dynamic filter for last 6 months
If I was understanding correctly, the requirement is to show monthly number of last 6 calendar months, excluding this month. I could not find a straightforward way for this. My solution may contain a bit of hacky scent.
Power BI does not have built-in filter support based on calendar months relative to now. We need to build a custom logic to achieve this. I did it by creating a measure that indicates whether current filter context is within the desired period. This measure is a flag that returns 1 if the filter context is a single calendar month which is included in the last 6 calendar months, or returns BLANK otherwise.
__Last6MonthFlag =
VAR YearMonths = CALCULATETABLE(
VALUES( 'Calendar'[Year Month] ),
REMOVEFILTERS( 'Calendar'[Year Month] ),
'Calendar'[Date] > EOMONTH( TODAY(), -7 )
&& 'Calendar'[Date] <= EOMONTH( TODAY(), -1 )
)
RETURN IF(
HASONEVALUE( 'Calendar'[Year Month] )
&& SELECTEDVALUE( 'Calendar'[Year Month] ) IN YearMonths,
1
)
Then I used this measure in the visual filter like this.
You need to do below activities to achieve your requirement.
Define a calendar table holding all the dates for your report. Define a calculated column for Month-Year. Month-Year = FORMAT('CalendarTable'[Date], "MM-YYYY")
You can define a calculated measure, which will return 1 if the end date of the contract is < 6 months from current date (you can use TODAY() function). This function will help in rolling calculation based on current date. Otherwise, this calculated measure will return NULL and SUM them.
You can drag the month-Year calculated column, defined in step no. 1, to the column axis. You can drag the calculated measure, defined in step no.2, to the values section. PowerBI control by default filters out the NULL values. So, when you use the calculated measure defined in step no. 2, you will get values only for the last 6 months. As the calculation is defined based on TODAY(), it will be a running calculation.
Trying to use a simple year to date (YTD) measure. But things are not working as expected.
Data Model
dim_date (the weekend column is incorrect, but doesn't affect of principle)
fact
The YTD measure is defined as the following:
YTD = CALCULATE(
SUM('fact'[value]),
DATESYTD(dim_date[Date])
)
which give me the following incorrect results for YTD.
It looks like instead of calculating the YTD for the weekend and weekday separate.
Given there are16 rows with that have a date that is label as a weekend as well as fact that all of them falls in the same year, I expect the YTD and valueto be identical in the above.
Interesting enough, if I slightly change the DAX by using the dim_date[Date].[Date] instead of just dim_date[Date], everything just works as expected.
YTD = CALCULATE(
SUM('fact'[value]),
DATESYTD(dim_date[Date].[Date])
)
Can anyone help to explain what is actually going on here?
The example .pbix file is here:
https://drive.google.com/open?id=1y3ndL7yDE7T4x7Z2bPhMsa-NHRgGzYj0
As dax.guide points out,
DATESYTD ( <Dates>[, <YearEndDate>] )
is equivalent to
DATESBETWEEN (
<DATES>,
STARTOFYEAR ( LASTDATE ( <DATES> )[, <YEARENDDATE>] ),
LASTDATE ( <DATES> )
)
So what's happening is that since Jan 10 is the last False value for weekend and Jan 12 is the last True value for weekend and the DATESBETWEEN function returns a continuous range not filtered by the weekend evaluation context, you get all dates up to Jan 10 in one case and all dates up to Jan 12 in the other.
To make the measure take into account the weekend value instead of calculating over a contiguous date range, you can add that as a filter:
YTD = CALCULATE(
SUM('fact'[value]),
DATESYTD(dim_date[Date]),
dim_date[weekend] IN VALUES(dim_date[weekend])
)