Power BI - SQL to DAX? - powerbi

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);

Related

When to use Power Query and DAX

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

How does one count the number of columns in a table in Power BI using Dax

In Power BI desktop this is my 'Table1':
How do I count the columns (not rows) using DAX? Ideally this would be a measure called ColCount.
The end results should be ColCount=4. I have tried invoked functions, but new tables are unwanted.
DAX doesn't really have a method for that.
PQ/M has this method:
let
Source = Table.ColumnCount
in
Source

M Power Query refer to DAX calculated table as a source

Is it possible to refer from Power Query (M) to DAX calculated table? I would like to get DAX table as a source to my power query.
The purpose. I have grouping table made in DAX. I would like to make econometric model with R. So I would like to transform the DAX table with R to get the model parameters. I would like to use these parameters further in DAX measures (not just display them).
Currently I dump the DAX grouping table to Excel file and then pull it up with Power Query.
Actually, there is a way.
DISCLAIMER: This is a hack. You should not rely on this way.
1. Create DAX calculated table
Input any DAX formula that evaluates to a table in Modeling > New Table.
2. Check port number using DAX Studio
Connect to your PBI Desktop data model using DAX Studio, and check the port number where the data model is hosted. It should be displayed in the right bottom of the window.
3. Import the table to Power Query
Click Get Data > Analysis Services and input the address (in my example "localhost:50293") to Server. Then navigate to your DAX calculated table.
it's not possible to refer to a DAX calculated table in M as it's loaded into DAX/Power Pivot engine after M has done the transformations. You can't write to a DAX table after loading into R as well. You can do grouping in M, or if needed run R in the Power Query. One approach that I have used has to load the data, duplicate the query, run a group/filter on the new query, then use that data in a later stage in the report.
Hope that helps
Jonee is correct. This is not possible. DAX calculated tables are computed after the M queries have loaded and you cannot feed them back into Power Query without saving them externally like you are currently doing.
The M language is more powerful than you might think and very likely could do the same grouping operations, though depending on what they are, it might be fairly difficult. You can also use R or Python script within an M query if you are more comfortable with those.

Find position of a character in a string function in M query vs find DAX

I tried to use the mid and find function in power BI as it can be done in excel. However, I get the error 'find' wasn't recognized.
After searching for a while a have a conclusion that FIND and MID function work in DAX (Excel and Power BI - but not in M query (edit custom) column). Instead of using find, in and M query we should use BIText, PositionOfAny.
Here is an example:
DAX:
MID([TRAFFIC_SIGNAL]), find([TRAFFIC_SIGNAL],"&"),3)
M query:
Text.Combine({Text.Start(Text.Upper([TRAFFIC_SIGNAL]), 3), " ",
Text.Middle(Text.Upper([TRAFFIC_SIGNAL]),
Text.PositionOfAny([TRAFFIC_SIGNAL], {"&"})+1, 3)})
It works so I would like to share because I haven't know the difference between DAX and m query in Power BI before, but this example helps.
I am not sure what you are looking for, but I am just going to highlight the differences between m query and DAX.
M Query:
M query is used to bring the data into the model
This can be accessed using the Power Query Editor
DAX:
DAX is used to create measures and columns after the data is pulled
This is mainly used for summarizing the data
Hope this helps.
M and DAX are entirely different languages used for different purposes.
Primarily,
M is how you get and transform your data before loading to the data model.
DAX is for reading the data in your data model aggregating it to show in visuals.
There are plenty of things you can do with both where it isn't clear which is the better option. It can be highly case-dependent but the above is a simple guide.
In any case, I wouldn't recommend that M code for that purpose. Something like the following should be simpler and more similar to the DAX code:
Text.Middle([TRAFFIC_SIGNAL], Text.PositionOf([TRAFFIC_SIGNAL],"&"), 3)

What's the difference between DAX and Power Query (or M)?

I have been working on Power BI for a while now and I often get confused when I browse through help topics of it. They often refer to the functions and formulas being used as DAX functions or Power Query, but I am unable to tell the difference between these two. Please guide me.
M and DAX are two completely different languages.
M is used in Power Query (a.k.a. Get & Transform in Excel 2016) and the query tool for Power BI Desktop. Its functions and syntax are very different from Excel worksheet functions. M is a mashup query language used to query a multitude of data sources. It contains commands to transform data and can return the results of the query and transformations to either an Excel table or the Excel or Power BI data model.
More information about M can be found here and using your favourite search engine.
DAX stands for Data Analysis eXpressions. DAX is the formula language used in Power Pivot and Power BI Desktop. DAX uses functions to work on data that is stored in tables. Some DAX functions are identical to Excel worksheet functions, but DAX has many more functions to summarize, slice and dice complex data scenarios.
There are many tutorials and learning resources for DAX if you know how to use a search engine. Or start here.
In essence: First you use Power Query (M) to query data sources, clean and load data. Then you use DAX to analyze the data in Power Pivot. Finally, you build pivot tables (Excel) or data visualisations with Power BI.
M is the first step of the process, getting data into the model.
(In PowerBI,) when you right-click on a dataset and select Edit Query, you're working in M (also called Power Query). There's a tip about this in the title bar of the edit window that says Power Query Editor. (but you have to know that M and PowerQuery are essentially the same thing). Also (obviously?) when you click the get data button, this generates M code for you.
DAX is used in the report pane of PowerBI desktop, and predominantly used to aggregate (slice and dice) the data, add measures etc.
There is a lot of cross over between the two languages (eg you can add columns and merge tables in both) - Some discussion on when to choose which is here and here
Think of Power Query / M as the ETL language that will be used to format and store your physical tables in Power BI and/or Excel. Then think of DAX as the language you will use after data is queried from the source, which you will then use to calculate totals, perform analysis, and do other functions.
M (Power Query): Query-Time Transformations to shape the data while you are extracting it
DAX: In-Memory Transformations to analyze data after you've extracted it
One other thing worth mentioning re performance optimisation is that you should "prune" your datatset (remove rows / remove columns) as far "upstream" - of the data processing sequence - as possible; this means such operations are better done in Power Query than DAX; some further advice from MS here: https://learn.microsoft.com/en-us/power-bi/power-bi-reports-performance