Use Global Parameters in Power Query - powerbi

I have a lot of tables where in each of them there is an int filter. I want to simply change that value from editing the parameter here
and reference it from each query of each table like this
Is there any way I can do this?
I've tried searching into Power BI docs but I have not found anything yet

Yes. Either inject the parameter value into the query through string manipulation, like this or use the parameter in a filter in a subsequent step and let "query folding" do its job. I don't know if query folding works when using SQL against Kusto/ADX, so you may need to use KQL instead.
This query demonstrates both approaches, the ago condition is injected into the query text because it can't be folded around the original query, but the filter is added through folding, and will be evaluated by Kusto:
let
KQL = "
SomeTable
| where TIMESTAMP > ago(" & Text.From(WindowDays) & "d)
",
Data = AzureDataExplorer.Contents("yourcluster.kusto.windows.net", "yourdb", KQL, [CaseInsensitive=true]),
#"Filtered Rows" = Table.SelectRows(Data, each [SomeColumn] = "SomeValue")
in
#"Filtered Rows"
And if you don't further filter or select rows the query sent to Kusto will be
SomeTable
| where TIMESTAMP > ago(2d)
| where SomeColumn = "SomeValue"
But normally your report will add additional filters and select particular columns.

Related

Change values from column in Power Query editor

I am working on a Power BI report and I am using a parquet file as the source. I have this column that holds info about the month:
As you see here, in MONTH_RUN, I have the data stored both as "202201" (yyyymm format) and "12/1/2021" (more like a date format).
I would need to change the values that appears like this: "12/1/2021" to "202112", so I can have all the values the same format.
I need to do this in a dynamic way, like searching for the rows that has values containing "/" character and based on that to change it into "yyyymm" format.
Is this possible?
I'm kinda new with power query and idk how should I implement this, but I would prefer to do it without creating any other additional columns.
Please, do not post sample data as images. It makes it difficult to answer your question.
One way to deal with this is to add a custom column, where the value will be either the same as MONTH_RUN (if it doesn't contain /), or it will parse the text to date, and then format it accordingly. For example:
if Text.Contains([MONTH_RUN], "/") then
Date.ToText(Date.FromText([MONTH_RUN], [Format="M/d/yyyy", Culture="en-US"]), [Format="yyyyMM", Culture="en-US"])
else
[MONTH_RUN]
Then use this custom column in your report.
I would just delete the existing MONTH_RUN column and create a new one based on the date in the DAY_RUN column.
eg:
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"MONTH_RUN"}),
#"Added Custom" = Table.AddColumn(#"Removed Columns", "MONTH_RUN", each Date.ToText([DAY_RUN],"yyyyMM"), type text)
Or, from the UI:

List Dates and customizing tables in Power Query

I'm trying to Add Custom Column in Power Query with the objective to return a Table from a List of dates.
The syntax used is as follows below:
= Table.AddColumn(TypeDate, "AddTable", each Table.FromList(
List.Dates([Date_begin],1,#duration(1,0,0,0)
)))
where:
TypeDate is the name of last step in Power Query
"AddTable" is the name of added custom column
[Date_begin] is a column with dates to be considered as the start of my list
Although the syntax seems correct, Power Query returns an error described as follows:
Expression.Error: We could not convert the value #date(2021, 1, 1) on to Text.
Details:
Value=01/01/2021
Type=[Type]
Does anyone know how to handle this problem?
I'll show an image where Power Query shows the error.
Select here to see Power Query interface
Your question is unclear
You want to add a column that has a table of dates for each row, using Date_Begin and Mes_Final?
#"Added Custom" = Table.AddColumn(TypeDate, "AddTable", each Table.TransformColumnTypes(Table.FromList({Number.From([Date_Begin])..Number.From([Mes_Final])}, Splitter.SplitByNothing(), {"date"}),{{"date", type date}}))

Conditionally Filtering Out Rows based on 2 Parameters in w/ Power Query

I have a table similar to the one attached below:
What I would like to do, using power query, is conditionally filter out (remove) rows where CenterNum = 1101 and DepCode = 257. I figured Table.SelectRows() would work but it doesn't and the query just returns, this table is empty. The #"Expanded AccountLookup" ,in my formula below, is referencing the power query applied step before the one I am trying to create. I'm hoping to get some input on how to remove rows based on these two paramters.
= Table.SelectRows(#"Expanded AccountLookup", each [CostCenterNumber] = "1111001" and [NoteTypeCode] = "257")
Thank you!
You didn’t post a screenshot so it is hard to tell if the column format is text or numerical but try removing the quotes around the numbers
= Table.SelectRows(#"Expanded AccountLookup", each [CostCenterNumber] = 1111001 and [NoteTypeCode] = 257)
If that doesn't work, check the actual column names against what you are using, especially for case (upper/lower) and leading/trailing spaces. The best way to do that is to temporarily rename it, and look at the code for the "from name"

Filtering other tables based on cross-highlighting a filtered measure

I want to count records in a certain condition and allow people to filter down to the relevant records, but selecting a measure value (which filters so it only counts certain rows) isn't cross-filtering others as I'd expect. Maybe ths isn't possible or maybe I'm just doing it wrong, but I'd appreciate help.
I have a single table:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wci4tLsnPTS1SMFTSUTJUitVBEjICChmgChljCplgajSFCMUCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Customer Name" = _t, #"Ordered Recently" = _t]),
#"Change to INT" = Table.TransformColumnTypes(Source,{{"Ordered Recently", Int64.Type}}),
#"Change to T/F" = Table.TransformColumnTypes(#"Change to INT",{{"Ordered Recently", type logical}})
in
#"Change to T/F"
The result looks like this:
Customer Name Ordered Recently
Customer 1 True
Customer 2 False
Customer 3 False
Customer 4 True
Customer 5 True
I added two measures:
Count Total = COUNTROWS(Customers)
Count Recent = CALCULATE([Count Total], filter(Customers, Customers[Ordered Recently]))
If I put both measures in a bar chart and highlight the "Count Recent" measure, I'd expect it to know to filter other visuals based on the FILTER statement present in the measure, but that doesn't happen. Selecing this value doesn't impact anything else on my page (including just a count of rows).
The goal is to allow people to select a measure that counts rows and then to see the makeup of the data in those rows (select a count of late projects and filter other visuals to describe those late projects).
Is this possible or am I doing something wrong?
EXAMPLE:
Here's what it looks like now, with nothing selected:
When I select the black bar (the "Ordered Recently" measure), nothing changes right now - but here's what I want to happen (actually achieved with a slicer off screen on the T/F field):
I understand if my measure is a SUM of an integer field, it includes every row in the calculation - even when the row value is zero - and there's no way to filter my dataset based on that. However, in this case, my measure is actually using a FILTER on the dataset so that it only counts rows with a certain criteria set - given that, it should be able to filter the requested table, and then flow that filter through the rest of my dataset (the same way it would if I selected a bar from a chart where I had used that same field as the series - exactly how it works when I do this:
PBIX file to download as an example
No, I don't believe it's possible to make a measure value cross-filter other visuals based on filters within the measure definition.
You can, however, click on i.e. row header Customer 3 and it should cross-filter the other visuals to only include that customer. Any table column you set for the rows or columns of a matrix visual should behave this way.
Here's a hacky workaround:
Create a measure that shows the right values when you use the column you want to use as a filter as the Legend or Axis (like in your last image). For example, in this case, you could do this:
Total Customers =
VAR TF = SELECTEDVALUE ( Customers[Ordered Recently] )
RETURN
COUNTROWS (
FILTER (
ALLSELECTED ( Customers ),
IF ( TF, TF, TF || Customers[Ordered Recently] )
)
)
This behaves how you want, but isn't labeled as you want. To achieve that create a calculated column with the labels you want. For example,
Label = IF(Customers[Ordered Recently], "Ordered Recently", "Total Customers")
Then take Ordered Recently off the axis and put the Label column in the Legend box to get this:
Your Filter argument is really Filter(All(Customers, Customers[Ordered Recently])
You remove all filters on the Customer Table, and then specify Ordered Recently Column as the filter.
Try
[MeasureName] =Calculate([Count Total], All(Customer), Values(Customer[Recently Ordered]), Customer[Recently Ordered] = “True”)

Power BI/query text column is shown as a table

I am importing data into Power BI desktop from an XML file. If the field value is empty, e.g.,
<Address></Address> then PBI assigns that column type "Table" and it is not possible to change the type into text. I am getting an error:
Expression.Error: We cannot convert a value of type Table to type Text.
Details:
Value=Table
Type=Type
It looks like if the first encountered value is empty, then all following rows of that column in the same "page" (data is being read in chunks a.k.a "pages" by the data connector) fetch are assigned type "Table", though not always.
For some columns, it even not possible to change the column data type (they have "table" icon)
Any ideas on how to change the column type to text?
Edit1: I have noticed that clicking "Table" actually shows there is a text value in that column. Maybe there is some invisible setting for Power BI to overcome this blank string value problem?
Edit2: it looks like I have the same problem as described here https://community.powerbi.com/t5/Desktop/XML-Import-Table-Type-in-Column/td-p/97512. There is no solution to this simple problem?
I found this https://community.powerbi.com/t5/Issues/Power-Query-should-not-wrap-simple-xml-elements-in-a-table/idi-p/125525. It looks it is "by design" and no solution exists. It's a pitty for wasting time...
I have found someone's solution:
let
ConvertTableField = (aTable as table, fieldName as text) =>
let
//alternative: https://community.powerbi.com/t5/Desktop/Expand-value-from-table/td-p/214838
//#"Expanded g_v" = Table.TransformColumns(#"Expanded ts_info", {{"g_v", each if _ is table then Table.FirstValue(_, "") else _}})
#"Lists from table" =
Table.TransformColumns(
aTable,
{
{
fieldName,
each
if _ is table then
Table.ToList(_)
else {_}
}
}
),
#"Expanded List" =
Table.ExpandListColumn(
#"Lists from table",
fieldName
)
in
#"Expanded List"
in
ConvertTableField
I haven't still figured out how to run this function for all the columns of the table. M language is the most difficult of the programming languages I know more or less...
I am not sure about the XML usage, but as far as table type to text conversion goes - this worked for me. Maybe it's an overkill.
let
Source = (Table1 as table) =>
let
Section = Record.ToTable(#sections[Section1]),
Compare =
Table.SelectRows(
Section,
each [Value] = Table1
){0},
GetName = Record.Field(Compare, "Name")
in
GetName
in
Source