When to use Power Query and DAX - powerbi

I am generating a Calendar table which can then be used to create a Gantt Chart visual on Power BI.
I have generated a list of dates in Power Query. Now I need the week number and year values for each of these dates.
The M query has a function
Date.WeekOfYear
, but this does not return the ISO Week number.
each Date.WeekOfYear([DateColumn], Day.Monday)
On the other hand, DAX has a function
WEEKNUM
which gives the ISO Week number.
ISOWeekNumber = WEEKNUM('Calendar'[DateColumn], 21)
I understood from looking up on the internet that DAX is more measures and calculated columns.
So, in this case, should I use Power Query or DAX if I want Year, Week Number, Week of the Day Number, etc.? What is the right thing to do and what are the implications of choosing one over another?
The algorithm to generate ISO week number looks complicated in Power Query and there seems to be no ready function to generate ISO week number.

Whether to generate this in DAX or PQ probably depends on what you are going to be doing with the results.
But it's not that difficult to generate it in Power query
Here is one function to generate ISO Weeknumber in Power Query (adapted from an Excel VBA function I used from before that function was added to the Excel library, originally developed, I believe, by Daniel Mahar).
//fnISOWeekNum
(theDate as date) =>
let
a = Date.AddDays(theDate,-1),
b = Date.DayOfWeek(a,Day.Sunday),
c = Date.AddDays(theDate,-b + 3),
d2 = #date(Date.Year(c),1,3),
IWN = Number.IntegerDivide(Number.From(theDate)-Number.From(d2) + Date.DayOfWeek(d2,Day.Sunday)+6,7)
in
IWN

Related

Getting date for calculation

I am a new user of Power BI and I was wondering if it is possible to calculate the date difference that is present in two different tables but they both are connected to the third table in which I want to create a measure or a column using dax to calculate date difference:
The columns marked in red are dates and I want their difference to be calculated in the assumption table. I used the month number to build the relationship between these tables.
You can use a Measure as below-
date_diff =
DATEDIFF(
RELATED(d_nl[notification]),
RELATED(d_fc[file_closure_date]),
DAY
)
To know about other Intervals rather than DAY, you can visit Here.

Power BI DAX Dynamic Calendar

I am trying to create a Dynamic Table in Power BI.
I have my set of data, and basically I want the calendar to pick up the minimum date and the maximum date from my table, but only if the maximum date is not the month of today. If the maximum date is the month of today, then it should ignore it and the calendar should be created with the max date of the previous month.
I started the formula, but can't seem to continue it. Any ideas?
Calendar_= CALENDAR(MIN('Table1'[Date]),IF(MONTH(MAX('Table1'[Date]))=MONTH(TODAY()),date(YEAR(MAX('Table1'[Date])),.....
See if this post helps: https://community.powerbi.com/t5/Desktop/Dynamic-table-or-on-fly-table-generation-via-DAX/m-p/434397#M200275. If not, I would simplify by creating some additional calculated columns or measures and then reference those instead of one nice big formula

Online trend of KPI in Power BI

let's say I have a KPI which is a simple calculation like
SUMX(FILTER(Table;Table[Date] < Today());Table[Column])
I would like to plot a time series of the KPI's past values (for example for the first day of every month since 01-01-2014), so for instance it is enough to create a new table - Trend_Table - with two columns - DATE, KPI and calculate each row's KPI value as
SUMX(FILTER(Table;Table[Date] < Trend_Table[Date]);Table[Column])
(it may not exactly work that way in DAX but you get the idea)
The problem is I really want to have online values, which means that once the September begins a new row with DATE = '01-09-2018' should be added automatically to the table 'Tren_Table'.
Is it possible in Power BI? Any referalls greatly welcomed.

Power BI DateDiff seems to return incorrect number of days

I'm using DAX DateDiff in Power BI to calculate the number of days between two dates, like this:
DaysDiff = DATEDIFF('MyTable'[Sales Order Date],
'MyTable'[Paid Date],DAY)
The formula is however returning some odd looking results, such as:
I would expect to see the actual number of days between the dates. For example, the number of days between 3/31/2017 and 12/4/2017 should be 248.
Both source columns are formatted as dates, and appear in the actual data as shown here.
How should the difference be calculated? I also tried a different formulation, which returns the same result:
DayDiff = 1.* ('MyTable'[Paid Date]-'MyTable'[Sales Order Date])
From the hint of the DaysDiff, my guess is that you have multiple records with same Sales Order Date and Paid Date, and Power BI has aggregated (default Sum) the results of all to one number.
If you change the summarization to either Average/Minimum/Maximum it should work fine.

Power BI - SQL to DAX?

I have an rdat field that I have to convert to a date field which I know how to do in sql but am new to DAX and I'm not sure how to do this
CONVERT(CHAR(10), DATEADD(d, RDAT_ENTERED +5843, 0), 1)
in Power BI. I can fix on the sql side so that it just brings in the data but I would still like to know how this would look in BI.
It seems your data uses the SQL SERVER EPOCH to represent dates so RDAT_ENTERED + 5843 means a determinated number of days from 1/1/1900. You can use that information to convert it to human dates via DAX or Power Query.
Create a calculated column either in Dax or Power Query (informally known as "M")
DAX in Power BI and Power Pivot
HumanDate = DATE(1900,1,1) + [RDAT_ENTERED] + 5843
M language in Power Query
=Date.AddDays(DateTime.FromText("1900-01-01"), [RDAT_ENTERED] + 5843)
I realize the context of your question and recognize the excellent answer by #alehandro. However, a few suggested best practices:
SQL is a BI tool
Transformations to standard data types should be at as low a level as possible
Magic numbers should not be used DECLARE #EPOCH_RDAT DATE = { d '1916-01-01' };
Function parameters should be used as intended: SELECT DATEADD(DAY, RDAT_ENTERED, #EPOCH_RDAT);