Comparing dates using month and year only - compare

I'm trying to compare two dates consisting of the month and year only. The month and year will be input by keyboard. The result should be the age.
Any help will be appreciated...Thank you

The logic behind this question is:
months = ( year 2 * 12 + months2 ) - ( year1 * 12 + months1 )
years = months / 12

A generic answer would be to make the DAY constant at 1.
So in VBA it would look like:
IF SERIALDATE('1',monthVAR1,yearVAR1) = SERIALDATE('1',monthVAR2,yearVAR2) THEN
--DO STUFF
ELSE
--DO OTHER STUFF
END IF

Related

Find last n days previous year DAX?

I am trying to find a way to calculate the sum of a value for the last n days of the previous year.
For example:
Lets assume n = 1
today's date = 31.05.2017, then my last date is 30.05.2017(as n=1) then the sum for 31.05.2016 should be calculated.
Lets assume n = 5
today's date = 31.05.2017, then my last 5 days would be from 25.05.2017 to 30.05.2017 and then the sum for previous year last 5 days 25.05.2016 to 30.05.2016 should be calculated
Hope somebody can help me. Thanks.
Here is some hints for generating the start and end date using DAX-
start_date =
var n = 5
return TODAY() - n - 365
end_date =
var n = 5
return TODAY() - 1 - 365
You can create them both inside a DAX and store them in 2 variable. Then calculate your desired value using the date range from start to end date.

Output value from Pervious Month (December) in January

Hopefully someone can help me.
I use the measure below to output a value from the previous month, however it appears to have broken this month (January).
In March it outputs Februarys value, and in April it outputs Marchs value. However as it is January it doen't appear to be outputting Decembers value.
Can anyone advise how I would adapt this measure to retrieve Decembers value?
Previous Month = var current_month= MONTH(TODAY()) return CALCULATE( AVERAGE('DATA_TABLE'[VALUE]), FILTER(DATA_TABLE,MONTH(DATA_TABLE[DATE])=current_month-1))
Thanks for your support.
Dax's Month commmand returns a number. In your case, you're asking for month 0, as january is month number 1. Since there is no month numbered as 0, it returns no data (blank).
You just need to add a condition for when the month is 0, to instead return data for the previous year's last month.
This is a possible solution:
VAR current_month = MONTH(TODAY())
VAR current_year = YEAR(TODAY())
VAR previous_month = current_month - 1
VAR previous_year = current_year - 1
RETURN
CALCULATE(
AVERAGE('Table'[Values]);
FILTER('Table';
IF(previous_month = 0;
MONTH('Table'[Date]) = 12 && YEAR('Table'[Date]) = previous_year;
MONTH('Table'[DATE]) = current_month - 1)
)
)
This way you accomodate for the 0 month value and filter according to it.
If it is not 0, the measure behaves as before. Otherwise it filters for the last month of the previous year.

Calculate Number of Working Days based on a Month and Year Column - DAX

I have a column like below for which I would want to extract the number of working days (excluding just weekends- Saturday and Sunday, holidays needs not be addressed).
As of now I just want:
Required_Column = Total No of Days in that month - No of weekends in that month
Month_Year
01-2018
02-2018
03-2018
...
...
01-2019
02-2019
I am a newbie in Power query and DAX , i tried looking up various methods using DAX however, could not find any relevant lead.
Expected Output:
Month_Year Required_Column
01-2018 23 (31-8)
02-2018 20 (28-8)
03-2018 22 (31-9)
... ...
... ...
01-2019 23 (31-8)
02-2019 20 (28-8)
Appreciate your help on this.
Although a Calendar table based approach is recommended as in the comment by RADO and Strawberryshrub, it is also possible to do this with DAX calculated column.
In the example below, I'm assuming MonthYear column contains the first day of each month.
WorkingDays =
VAR Year = YEAR( [MonthYear] )
VAR Month = MONTH( [MonthYear] )
VAR DatesInMonth = GENERATESERIES( [MonthYear], DATE( Year, Month + 1, 1 ) - 1, 1 )
RETURN SUMX(
DatesInMonth,
IF( WEEKDAY( [Value] ) IN { 1, 7 }, 0, 1 )
)

Calculated Column with Current Row Values and Previous Dates in Power BI (DAX)

I'm trying to get a calculated column (not a measure) that gets the sum of a column based on the values in the current row of that table for dates that are 1 month lagged to the date on the current row. My table has dates that are the 1st day of every month only .. no other days in the month. I'm asking the question about DAX; however, I have no problem implementing in M Language in Power Query (actually would probably prefer) if there is a solution that way as well.
I have been able to get a measure to work using something like this..
CALCULATE(SUM(AMT), DATEADD(DATECOLUMN, -1, MONTH))
But I'd like to be a new column instead.
Assuming the table looks something like this..
A B C D AMT
6 BAC456 5/1/2019 TEST 25
2 EPS123 4/1/2019 TEST 45
2 EPS123 3/1/2019 TEST 65
6 BAC456 4/1/2019 TEST 43
6 BAC456 4/1/2019 TEST 88
7 GRE123 4/1/2019 TEST 90
9 BAC456 4/1/2019 TEST 43
I'd like to have another column in this table where the first row would be:
A B C D AMT NEWCOL
6 BAC456 5/1/2019 TEST 25 131
Second row would be:
A B C D AMT NEWCOL
2 EPS123 4/1/2019 TEST 45 65
etc..
In cases where the month column is the first month in the entire table NEWCOL would be 0
To get 131, I'm assuming that you are requiring a match on both columns A and B.
NewCol =
CALCULATE (
SUM ( Table1[AMT] ),
ALLEXCEPT ( Table1, Table1[A], Table1[B] ),
PREVIOUSMONTH ( Table1[C] )
)
This sums the column AMT keeping the row context of columns A and B and specifies the previous month as a filter on C. Note that this returns a blank for rows that don't have a previous month. If you'd prefer 0 then add + 0 after the last closing parenthesis.
If PREVIOUSMONTH doesn't work, then try this:
NewCol =
CALCULATE (
SUM ( Table1[AMT] ),
ALLEXCEPT ( Table1, Table1[A], Table1[B] ),
Table1[C] = EOMONTH ( EARLIER( Table1[C] ), -2 ) + 1
)
For date = 5/1/2019, EOMONTH ( date, -2 ) returns 3/31/2019. Add one day to get 4/1/2019.
To achieve this easily in a calculated column, you need something like this before writing the final DAX.
Month Num = MONTH(MyTable[C])
Month Diff = DATEDIFF(MyTable[C],MAX(MyTable[C]),MONTH)
And now you have these Month differences and Month numbers defined, you can write a DAX like this -
Amount - New Column =
Var selectedValue_B = MyTable[B]
Var SelectedValue_MonthDiff = MyTable[Month Diff]
Var out1 = CALCULATE(SUM(MyTable[AMT]), FILTER(ALL(MyTable), MyTable[Month Diff] = SelectedValue_MonthDiff+1 && MyTable[B] = selectedValue_B)) + 0
return out1
This makes my table to look something like,
I have used Var(Variables) in my formula to help you understand what is happening inside the formula.
Kindly accept the answer if it solves your problem.

Calculate sales of products that sold this year but not the year before

I want to calculate the sum of NetSales for the products that had sales this year (2019) but NOT sales last year (2018).
This is what I'm trying (and a million variations more similar to this):
NetSales CY Not LY =
VAR ThisYear= 2019
VAR YearBefore= 2018
VAR TabelaThisYear = SUMMARIZE(FILTER(SUMMARIZE('Facts';Facts[ArticleNo];Facts[InvoiceDate];Facts[NetSalesCurr]);YEAR('Facts'[InvoiceDate])=ThisYear && Facts[NetSalesCurr]>0);Facts[ArticleNo])
VAR TabelaYearBefore = SUMMARIZE(FILTER(SUMMARIZE('Facts';Facts[ArticleNo];Facts[InvoiceDate];Facts[NetSalesCurr]);YEAR('Facts'[InvoiceDate])=YearBefore && Facts[NetSalesCurr]>0);Facts[ArticleNo])
VAR ProdutosOnlyThisYear = EXCEPT(TabelaThisYear;TabelaYearBefore)
RETURN
CALCULATE(SUM(Facts[NetSalesCurr]);ProdutosOnlyThisYear)
I would use the following approach, where we don't hardcode the years:
NetSales CY Not LY=
CALCULATE(
SUM(Facts[NetSalesCurr]),
FILTER(
VALUES(Facts[ArticleNo]),
CALCULATE(
COUNTROWS(Facts),
PREVIOUSYEAR(Calendar[Date])
) = 0
)
)
Let's break it down:
The inner FILTER function iterates through VALUES(Facts[ArticleNo]) - that is all ArticleNo's that have sales in the currently selected period.
For every one of those ArticleNo's, we calculate the number of rows on the fact table for the previous year. CALCULATE(COUNTROWS(Facts), PREVIOUSYEAR(Facts[InvoiceDate]))
We keep only those ArticleNo's where this number is = 0.
The list of ArticleNo's returned from FILTER is then used in the outer CALCULATE statement, so that we only get the sum of NetSales for articles that did not have any sales in the previous year.
For more information, please see the New and returning customers-pattern.
you can use the dax formula like at the below , if you have a calendar table ;
this year = CALCULATE ( SUM[measure];DATEADD(CALENDAR[DATE]),0,YEAR)
last year = CALCULATE ( SUM[measure];DATEADD(CALENDAR[DATE]),-1,YEAR)