PowerBI-DirectQuery - Separate Time From DateTime - powerbi-desktop

i currently have a DirectQuery set up on a SQL Server. There you will find field in the format "dd.mm.yy H:i:s".
Is it possible to separate the time from the date? So that I can set a filter?

If your column name is DateTime, you can separate Time from them in Power Query Editor. Just add a custom column with this below code shown-
= Time.From([DateTime])
OPTION 2
You can also split the value using Space (" ") which will separate Date and Time in 2 column in power query-
Output will be as below-

Unfortunately, what mkRabbani suggest does not work with DirectQuery connection - you cannot split column nor use "Time.From()" and preserve DirectQuery - Power BI will immediatelly suggest turnig into Import mode to apply the changes...
If you don't need it at the Power Query level but at the model level, you can create calculated column which will equal to your original Date-Time column. This calculated column then could be set to Date or Time data type.
Miro.

If we want to separate date and time from the DateTime columns in the DirectQuery, you can do it by changing the SQL query while importing the data.
Go to Power Query Editor and click on the settings icon in Applied Steps.
Data source setting
Then, click on advance options in the SQL server database setting and add the below SQL statement for your table. Change the column name based on your table column name
SELECT *,
[CreateDate_Date] = CONVERT(date, [CreateDate]),
[CreateDate_Time] = CONVERT(time, [CreateDate])
FROM [dbo].[Table]
SQL Query
Choose the Server and Database name and click Ok

Related

Reference an excel cell in a Where statement to change the Query

I would like to use an Excel cell to change the reference data in a Where statement so that I don't have to keep going into power query to change the statement.
Instead of the 31690 in the below code I would like to reference cell B7 in sheet1 of the same Workbook instead.
Is this possible? and if so how?
Thanks in advance.
WHERE ORDERDATE >= #Month13#(lf)#(tab)and STOCKCODE is not null#(lf)#(tab)AND SALESORD_HDR.ACCNO = '31690'
Maybe something like this?
For this approach to work, you need to make sure your spreadsheet has a table and the table's range starts with A1 and spans beyond the cell with the value in it--in this case, B7. Here's an example:
I started by creating this spreadsheet with a table named Table1:
Then, I used Table1 as the source in Power Query.
Notice that with the table above, what was row 7 is row 6. This is because the column headers don't have row numbers in Power Query. This change in row numbering matters for finding your targeted cell.
Then I added some custom M code. This code first extracts the second column's name from the list of column names. (Because the second column would be column B of the spreadsheet.) Then it uses that second column's name to create a table of that column's values, from which it then extracts the sixth row entry. (Because that sixth row entry would be the seventh row entry in the spreadsheet.) Note that the {1} points to the second column and the {5} points to the sixth row. That's because Power Query indexing starts at 0.
I went into Advanced Editor and renamed the step from Custom to DateVariable:
DateVariable = Table.Column(Table1_Table, Table.ColumnNames(Table1_Table){1}){5},
Then I added some more custom M code to concatenate the DateVarable with the rest of your SQL statement as an example:
Here's my M code:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
DateVariable = Table.Column(Source, Table.ColumnNames(Source){1}){5},
SQL_Statement = "WHERE ORDERDATE >= #Month13#(lf)#(tab)and STOCKCODE is not null#(lf)#(tab)AND SALESORD_HDR.ACCNO = '" & Text.From(DateVariable) &"'"
in
SQL_Statement
Simple solution using a named range
First, select cell B7 and enter a name in the Name Box (e.g. CellReference). Then right-click on the cell and click on Get Data from Table/Range.
This opens the Power Query Editor with a query that returns a table containing the cell from the named range. Open the advanced editor, delete the entire content of the query and type Text.From(Excel.CurrentWorkbook(){[Name="CellReference"]}[Content][Column1]{0}) and click on Done, this is what it should look like:
Note: Text.From() is used so that value returned by CellReference can be concatenated with the SQL query using &. Also, this function is preferable to Number.ToText() which does not work with text values.
Finally, insert the query name in your SQL query: WHERE ORDERDATE >= #Month13#(lf)#(tab)and STOCKCODE is not null#(lf)#(tab)AND SALESORD_HDR.ACCNO = "&CellReference
Note that if the cell contained a text value instead, then you would need to adjust the syntax like this: ... SALESORD_HDR.ACCNO = '"&CellReference_Text&"'"
How to deal with warnings: new query permission and Formula.Firewall
How to give permission to run all new native database queries
Depending on your Query Options settings, you may get this warning message regarding the permission to run the modified SQL query each time CellReference contains a new value:
If you are certain that the cell will never contain a string of characters that could modify the database, you can disable this warning message by going to File -> Options and settings -> Query Options. Under GLOBAL, go to Security and uncheck Require user approval for new native database queries.
Note that this is a global setting that is immediately applied to all your Excel files, including those that are currently open.
How to disable the Formula.Firewall warning message
Depending on your Privacy Levels settings, you may get a Formula.Firewall warning message preventing the query from being executed:
If you are in a situation where you can disregard privacy levels, you can disable this message by going to File -> Options and settings -> Query Options. Under CURRENT WORKBOOK, go to Privacy and select Ignore the Privacy Levels and potentially improve performance.
Click on OK and refresh the query.
If, on the other hand, your workbook needs to preserve a privacy level of Private or Organizational, to my knowledge there is currently no way of integrating CellReference to a SQL query (even using a SQL parameter set with the Value.NativeQuery function or a Power Query Parameter ) without raising this warning message. The only solution would be to include CellReference in another step in the query, but then the filtering will occur in Power Query and not at the server level: query folding is interrupted when a step includes a query/function/parameter that is linked to an external data source including a named range in the workbook itself.
If your workbook privacy level is set to Public, you should be able to avoid this warning message by using the Value.NativeQuery function (you can even enable query folding for further query steps if you are using a SQL Server or PostgreSQL database). If you still get the warning message, you can try combining the two queries accessing each data source (the database and the worksheet) into a single query.
Note: these steps were tested with Excel Microsoft 365 (Version 2107) on Windows 10 64-bit connected to a local SQL Server 2019 (15.x) database.
This answer was prepared by referring to many blog posts by Chris Webb (linked above) and by Ken Puls (like this one).

How to create a new table from Query data and change rows to column in Power BI

I am new to Power BI. I have searched a solution, but I can't find the one match my issue.
I use direct query to SQL Server to generate data table in Power BI. My query result as below:
And I want to create a new table and transform the data to be as below:
Really appreciate any help or idea.
Thanks
You need to pivot the Factory column to get the desired result.
In Power BI Desktop designer, click Transform data button from the Home ribbon tab to open Power Query Editor. First, delete the Comment column (or don't select it at first place) by right clicking on it's title and select Remove. You don't need it. Then select Factory column and click Pivot Column button from Transform tab:
and select Volume for values column:
This will pivot the column, i.e. make a separate column for each of the values in it, and give you the desired result:
In addition (you said you are using a DQ to get the data), you can also do the same transformation in the database query too.

How do I pass parameters to my SQL statement?

How can I use parameters to create a dynamic SQL statement? I specified two parameters "DateStart" and "DateEnd" which I want to include in my Data source's SQL statement, but I don't know what the proper way to reference them is.  My aim is to have the users be able to adjust the parameter dates at the opening of the report.
For Example:
Select projectid,projectname,date,agent,count(*) from production Where
fromdate = '04/01/2020' and to date = '04/05/2020" Group by Projectid,
Projectname,date,agent
Now, dates filters will be added in Excel cells and based on inputs result will appear on the table.
Also, advise will that work in MS Office 2016 & O365 version as well.
Thank you in Advance!

Querying single data points from the Excel Data Model / Power Query (Get & Transform Data)

I'm using an up-to-date version of Excel 2016 (via O365 E3 license) and using Power Query / Get & Transform Data. I can successfully create queries and load them to the page. I have also successfully created Power Pivot reports.
I would like to query single data points from the data loaded via Power Query. For instance, imagine a dataset called DivisionalRevenue with:
Date Division Revenue
2016-01-01 Alpha 1000
2016-01-02 Alpha 1500
2016-01-01 Beta 2000
2016-01-02 Beta 400
I could easily load that to an Excel workbook or include it in the data model and create a power pivot. However, Power Pivot doesn't always meet my requirements, particularly around how the data is displayed on the page. In order to achieve my goal I may want to be able to query individual data points.
I would like to have a cell on the page with a formula in it that I can use to query individual data points. If it was in a pivot table I could use something like:
=GETPIVOTDATA("Revenue",$A$3,"Date",DATE(2016,1,1),"Division","Alpha")
The lookup values (date and division) could be retrieved from a cell on the page or hard-coded into the formula. This is a requirement for several reports I'm working on.
Or, I could add a combined lookup column with Date and Division concatenated and use a vlookup to pull the values like:
=VLOOKUP("42371Alpha",I9:L13,4,FALSE)
Finally, I could use a combination of INDEX and MATCH to identify the correct row number and then pull the data.
All of these solutions require the data to be loaded onto a sheet. One requires a pivot table that has to be refreshed to work properly. The other two require creating arbitrary lookup columns so that you can match a row based on more than one field (date and division in this example), and you have to ensure that that lookup field's formula is properly extended down the length of the data table. In both cases I would have concerns when sharing this workbook with my colleagues in case someone affects the rather fragile setup of the pivot table or the lookup.
So, what I truly want to find is something equivalent to pivot table querying against a dataset.
** This doesn't exist, but I would like to know if something like it does **
=GETQUERYDATA("Revenue","DivisionalRevenue","Date",DATE(2016,1,1),"Division","Alpha")
Does such a thing exist? Can such a thing be done? Can I retrieve arbitrary data points from the dataset created through Power Query / Get & Transform Data?
I think that what you want are cubefunctions:
Some Background
How to easy create cubefunctions from a pivot table
There is a feature in Excel that allows you to query off of a PowerPivot model, but it's not highly advertised for some reason.
Once you have the data in your PowerPivot model, go to your Excel -> Data tab -> Existing Connections -> Tables tab
From there, choose the table that you want to start with. Once that table's data is on your excel sheet, you can actually right click that table -> go to "Table" -> "Edit DAX"
From there you can enter the following DAX function, as an example
EVALUATE
FILTER(SampleData,[Date]=DATE(2016,1,1) && SampleData[Division]="Alpha")
Make sure to choose Command Type=DAX in the drop-down. Here's how it looks on my screen:
To further improve your querying power, you can install the optional "DAX Studio" plugin for Excel, which allows you to write custom DAX queries and then export the results directly back to an Excel sheet.

Excel and tableau doesn't detect date dimension as datetime column

I have a DIM Date dimension in my cube that has a date column whose type in SQL datamart is "date" and the data type in cube for this arribute is set to "date". But when I query the cube from excel and tableau, this arribute shows up as string instead of date and so I don't get the natural hierarchy of year ->month -> date in the client tools. Both excel and tableau format this column correctly if I connect directly to datamart instead of a cube.
Is there any trick or tip to make these client tools format the date column as datetime instead of string ?
I don't want to manually create these hierarchies in the cubes because there are 60+ date columns in my cube across all dimensions
Thanks
In Tableau, you can change the field type to date and then save the data source as a reusable tds file. The settings should then be retained
From http://www.theinformationlab.co.uk/2013/12/02/tableau-file-types-and-extensions/ :
Tableau Datasource (.tds)
When you connect to your data for the fist time, you may have a little bit of data ‘modelling’ to do – setting the right data types, changing default aggregations, setting default colours, creating some custom calculated fields etc etc. You are giving Tableau information about the data you will be using – you are setting up its ‘metadata’. When you want to connect to this data again, you don’t want to really go through all this data modelling a second time so instead you can save your metadata as a .tds file (again, it is saved in XML format) and connect to your data though this file instead. You could also distribute this file so that your colleagues have access to the nice formatting and custom fields you have worked to set up.
Tableau is clever enough to pick up new columns/fields in the data source if they appear and column ordering does not matter but if column names change or disappear completely, you will need to reconfigure.
To create a .tds file, from Tableau Desktop, right click on your data source connection and select Add to Saved Data Sources. Alternatively you can publish the .tds to Tableau Server by right clicking and selecting Publish to Server instead

Resources