DAX measure with DATEADD function used for collecting data for last month from present month reports not working on year transition period - time

I've got filtering Calendar table for the date I collect reports monthly and I've got Table for calculating staff days on site per visit (DOS). Following code works nicely for all months but December. Please advise changes:
Completed Days on Site DOS =
CALCULATE(
SUM(Table[# Actual DOS]),
DATEADD(Calendar[Deployment Date],1,MONTH), //*Data from the report from one month ahead
IF(
MONTH(Table[Data Deployment Date]) = 1,
YEAR(Table[Visit Date]) = YEAR(Table[Data Deployment Date]) + 1,
YEAR(Table[Visit Date]) = YEAR(Table[Data Deployment Date])
),
MONTH(Table[Visit Date]) = MONTH(Table[Data Deployment Date]) - 1
)

Stupid mistake, instead of this line
MONTH(Table[Visit Date]) = MONTH(Table[Data Deployment Date]) - 1
should be
MONTH(
DATE(
YEAR(DOS_History_UpToDeployDate[Visit Date]),
MONTH(DOS_History_UpToDeployDate[Visit Date]),
DAY(DOS_History_UpToDeployDate[Visit Date])
)
) =
MONTH(
DATE(
YEAR(DOS_History_UpToDeployDate[Data Deployment Date]),
MONTH(DOS_History_UpToDeployDate[Data Deployment Date])-1,
DAY(DOS_History_UpToDeployDate[Data Deployment Date])
)
)

Related

Parallel Period Explanation

I am fairly new to parallel periods and there use in dax. I understand the basics of parallel periods but I am not familiar using them. I am attempting to understand some code used by my company if anyone wouldn't mind breaking this down for me I would greatly appreciate it.
I understand the range it is pulling between but fail to understand how the initial max calendar year = 2021 works in all this. Also, if you can help explain how dividing the first set of code by the second works. That would be a freakin life saver. The end result currently is it comes out to equal a percentage that is used currently.
IF(
MAX('Calendar'[Year]) = 2021,
CALCULATE( [Total Cash] , PARALLELPERIOD('Calendar'[PERIOD_ENDING] , -24 , MONTH ) )
,
CALCULATE( [Total Cash] , PARALLELPERIOD('Calendar'[PERIOD_ENDING] , -12 , MONTH ) ))
IF(
MAX('Calendar'[Year]) = 2021,
CALCULATE( [Total Cash] , PARALLELPERIOD('Calendar'[PERIOD_ENDING] , -2 , YEAR ) )
,
CALCULATE( [Total Cash] , PARALLELPERIOD('Calendar'[PERIOD_ENDING] , -1 , YEAR ) ))

Microsoft Power BI - DAX Time Intelligence measure - change context to reflect proper % change; non-YTD measures

I have a Power BI visual as below. There are 3 matrices. I have a DateDimension (or) Calendar table called Dates2.
I use two measures, one a regular measure (called 'Count'), the other a parallel period comparison of the measure (called 'Count_PreviousYear'). I use SAMEPERIODLASTYEAR DAX function for the latter.
1)
Count = COUNTA(TableX[ColumnY])
--Measure with name 'Count'--
2)
Count_PreviousYear = CALCULATE
(
[Count],
SAMEPERIODLASTYEAR(Dates2[Date])
)
--Measure with name 'Count_PreviousYear'
--this measure uses Time Intelligence function - SAMEPERIODLASTYEAR--
Both 'Count' and 'Count_PreviousYear' (obviously) are not YTD (YearToDate) values.
A third measure for the percentage change across periods is computed as below:
3)
PercentageChange = IF(
ISBLANK([Count]) || ISBLANK([Count_PreviousYear]),
BLANK(),
(([Count] - [Count_PreviousYear])/[Count])
)
Kindly ignore the fact that a keyword used as a measure name;
I have used the name 'Count' only for clarity; in my actual report,
I have proper names
The % change measure works fine, but one issue:
For the period change from 2020 to 2021, i.e. in the third row of the last matrix (for the row value 2021), the total (i.e. the % change value) is not appropriate.
I need to replace -737.21% with - 23.98 %.
This is because , I need to compute the Total for 2020, only by adding the values for the months of January and February, i.e. 428 + 430 = 858. (not 5794, which is for all the 12 months).
Since 2021 has only two months - January and February, I don't want to compare two months of 2021, with all the 12 months of 2020. Rather, I want two months of 2021 to be compared with the corresponding 2 months of 2020.
Essentially I need {(692-858)/692} * 100 = -23.98%
Currently, I see {(692-5794)/692} * 100 = -737.21%
Can someone help me achieve this?
Count Previous Year =
IF (
HASONEVALUE ( Dates2[Month] ),
IF (
[Count] <> BLANK (),
CALCULATE ( [Count], SAMEPERIODLASTYEAR ( Dates2[Date] ) )
),
IF (
HASONEVALUE ( Dates2[Year] ),
CALCULATE (
[Count],
DATESBETWEEN (
Dates2[Date],
EDATE ( MIN ( Dates2[Date] ), -12 ),
EOMONTH ( MAX ( [FactTable[Date] ), -12 )
)
)
)
)
Count_PreviousYear = IF (
(HASONEVALUE(Dates2[Year]) = TRUE && HASONEVALUE(Dates2[MonthName]) = TRUE),
CALCULATE
(
[Count],
SAMEPERIODLASTYEAR(Dates2[Date])
),
IF (
(HASONEVALUE(Dates2[Year]) = TRUE && HASONEVALUE(Dates2[MonthName]) = FALSE),
CALCULATE (
[Count],
DATESBETWEEN (
Dates2[Date],
EDATE (MIN(Dates2[Date]), -12),
EOMONTH (MAX(SourceData[Date]), -12)
)
),
BLANK()
)
)
!Output obtained as desired]1

Power BI - Compare Measures over Period

I'm currently working on a PowerBI script, which gives me headaches. I introduced a measurement, which shows the amount of sales for every company in a corporation. Which basically looks like this.
MarketShare =
DIVIDE(
CALCULATE(
SUM('Monthly Reports'[Active Certificates])
),
CALCULATE(
SUM('Monthly Reports'[Active Certificates]),
ALL('Monthly Reports'[Institute])
)
)
The output of this is a beautiful matrix showing me the marketshare of every company compared to the total sales.
Now I should display in a matrix below, (or the same, even better) the change of market share for every company every month. Hence, July should display a blank cell since it has no data before. August should display in the first line +0,34 in September +0,3 ... etcetera for every entry in line one and continuing down.
I tried to implement a measure, which deducts always the values from the previous one, but I have not been successful so far.
Market Share Prev Period =
(
DIVIDE(
CALCULATE(
SUM('Monthly Reports'[Active Certificates])
),
CALCULATE(
SUM('Monthly Reports'[Active Certificates]),
ALL('Monthly Reports'[Institute])
)
)
)
-
(
DIVIDE(
CALCULATE(
SUM('Monthly Reports'[Active Certificates]),
DATEADD('Monthly Reports'[LoadDate], -1, MONTH)
),
CALCULATE(
SUM('Monthly Reports'[Active Certificates]),
DATEADD('Monthly Reports'[LoadDate], -1, MONTH),
ALL('Monthly Reports'[Institute])
)
)
)
I get weird results at the moment. If I choose to filter by date and add the entire date the following table is displayed, it shows the correct differences for the middle dates, however not for the final date:
When I filter by date hirarchy and months (like in the other table above) I get exactly the same results again.
Thanks,
Vincenz
In your calculation you use ALLEXCEPT.
ALLEXCEPT removes the filters from the expanded table specified in the first argument(remove from all column in this table), keeping only the filters in the columns specified in the following arguments. That means you keep context from your matrix (July from your header). conditions are mutually exclusive.
You want to do the opposite thing, remove the filter from your "date" column. Use ALL('TableName'[ColumnName])
Your first measure should looks like:
Simple =
DIVIDE(CALCULATE ( SUM ( 'Monthly Reports'[Sales] ) )
, CALCULATE (
SUM ( 'Monthly Reports'[Sales] ),
ALL ( 'Monthly Reports'[Company] )
))
I solved my calculation and found a workaround for the wrongly displayed date hierarchy.
Sometimes the answer to a question if you're not familiar with a programming language yet is simpler as one could expect.
Finished working code:
Market Share Prev Period =
IF(
CALCULATE(
SUM('Monthly Reports'[Sales]),
PREVIOUSMONTH('Monthly Reports'[SaleDate])
)
<> BLANK()
,
(
DIVIDE(
CALCULATE(
SUM('Monthly Reports'[Sales])
),
CALCULATE(
SUM('Monthly Reports'[Sales]),
ALL('Monthly Reports'[Company])
)
,
0
)
)
-
(
DIVIDE(
CALCULATE(
SUM('Monthly Reports'[Sales]),
PREVIOUSMONTH('Monthly Reports'[SaleDate])
),
CALCULATE(
SUM('Monthly Reports'[Sales]),
PREVIOUSMONTH('Monthly Reports'[SaleDate]),
ALL('Monthly Reports'[Company])
)
,
0
)
),
"-"
)
I figured out, that be using DateAdd(.., -1, Month) I only changed what was read, not in which column it goes. So I was always missing the most recent value, as off course it was not read by the -1 operation and I was missing the oldest report as there was none before off course.
Basically I found the function: PREVIOUSMONTH (too simple to be true).
Furthermore, I added an IF clause before to check if I'm at the first column. If so, I'm dashing the value.
My only problem, which was left in the end, was that when displaying the date hierarchy I only saw dashes in all the fields of the matrix. However, if I displayed the whole date I could see all correct values. My workaround was to format the date with "mmmm". This displayed the same. I hope I will not need the date in any further step, as I will need to come back to this again.
So my result looked exactly perfect.

Month Differences in qlikview

There is 2 dates column one is from date and second is to date .. and i want to get month difference from these two dates
like if
from date to date month difference
01-02-2019 02-02-2020 13
here 02 (feb) month 2019 till 02 (feb) moth 2020 so this means total 13 months covered..
i tried this but this shows wrong results
month(from date) - month(to date)
and i also try this
month([from date] - [to date])
I've been using the code below for this case.
It basically converts both dates to months and returns the difference.
First the Year component of the date is "converted" to months (year([to date]) * 12 part) and second adds the month number of the date (month([to date])
Num (
( (year([to date]) * 12) + month([to date]) )
- ( ((year([from date]) * 12) + month([from date])) ) + 1
)
UPDATE:
below is a screenshot of the result table with 2 expressions - including the +1 and excluding it. Depends how you want to calculate the full months +1 will "include" the last month as well

Date Logic - Tableau

Could someone help me achieve the date logic as below:
I work for a fiscal year starting from Oct to Sep, once I finish the fiscal year and step into a new one I want the last month of last fiscal year's sales for reference until the end of new fiscal year. For example, now in Sep'17 the fiscal year ended but I want Sep'17 sales number to be shown in graph as reference until next Sep'18 later that I want Sep'18 number to be shown until Sep'19 and so on so forth.
The logic I have arrived is not a permanent solution as it requires editing once I step into Year 2018, it is as below:
IF YEAR([Invoice Date]) = YEAR(TODAY()) AND
DATEPART('month', [Invoice Date]) = 9 THEN [Sales] END
once I step into Year 2018, I need to make change to the above logic like:
IF YEAR([Invoice Date]) = YEAR(TODAY())**-1** AND
DATEPART('month', [Invoice Date]) = 9 THEN [Sales] END
Is there a way to achieve permanent solution without editing the logic?
Try this:
Create 2 calcualted fields for start date and end date, Where start date is september last year and end date is september current year.
Start Date:
DATEADD('month',-1,MAKEDATE(YEAR(TODAY())-1,MONTH(TODAY()),01 ) )
End Date:
DATEADD('month',-1,MAKEDATE(YEAR(TODAY()),MONTH(TODAY()),01 ) )
Now one more calcualted field which will create the filter and add the formula to filter to get the require data.
[Order Date]>=[Start Date] AND
[Order Date] <=[End Date]
Add to filter and then select True
Note: Here today function means start of the fiscal year that you need to manage.

Resources