I am facing the issue of reusing neat Excel formula for MAXIFs in Power Query M language.
The formula itself consists of several conditions regarding columns in Table2 and a value of interest (VOI) in Table1 (both being Excel table objects).
formula in Table1:
=MAXIFS(Table2[columnA],Table2[columnB],"criteriaB1",Table2[columnC],[#[VOI]],Table2[columnA],"<="&MINIFS(Table2[columnA],Table2[columnB],"criteriaB2",Table2[columnC],[#[VOI]])
(I will divide the formulas into lines to make reading easier)
=MAXIFS(Table2[columnA],
Table2[columnB],"criteriaB1",
Table2[columnC],[#[VOI]],
Table2[columnA],"<="&MINIFS(Table2[columnA],
Table2[columnB],"criteriaB2",
Table2[columnC],[#[VOI]])
So far I've been trying merging Table1 with Table2, grouping by some of the columns but as result I receive chunks of data that I can't/don't know how utilize in next steps. I simply cannot see the complete landscape of the procedure in Power Query M language.
Any help would be appreciated.
The corresponding idea would be to take a maximum over a filtered table.
For example, the MINIFS part would look roughly like this:
MinA =
List.Min(
Table.SelectRows(
Table2, each [ColumnB] = "criteriaB2" and [ColumnC] = "VOI"
)[ColumnA]
)
It gets a bit trickier since you need to pass the current row value of Table1[VOI] into the second condition but it's still doable and might look something like this:
AddMinAColumnToTable1 =
Table.AddColumn(
Table1, "MinA",
(Tab1Row) =>
List.Min(
Table.SelectRows(
Table2, each [ColumnB] = "criteriaB2" and [ColumnC] = Tab1Row[VOI]
)[ColumnA]
)
)
I recommend reading this blog post for a better understanding of the each and (_) => constructions.
Related
I want to reference all the data in my dynamic table, except for the first two columns. My goal is to return the header of the first column that isn't blank, starting with the third column. I have the formula figured out for everything except the starting with the third column part. Is there an easy way to accomplish this? I'm thinking I might have to just do something like
`=Table[#Data] unless in the range of the first two columns'
Hoping for an easier way though.
EDIT: if my request isn't clear enough, I am looking for a formula that would produce the following exact situation in these circumstances. It must work in a table that can change size without issue, it must ignore the first two columns, it must scan a complete column of data from left to right before moving onto the next column (most of the formulas I've tried would give the result Aug-21 here), and it must return the header in basically any format.
I don't have the time to write up a full answer for this, but you should use the "From Table" button "Get & Transform" section of the data ribbon.
Then, in the query editor window, In the home ribbon, click Manage Reference.To find the position of the first non-blank column will be hard, requiring learning Power-Query language, probably something like clicking the advanced editor and adding steps like
let
Source = #"YourSourceQueryName",
ColumnNames = Table.ColumnNames(Source),
ColumnsToRemove = 2 + List.PositionOf( // PositionOf is zero-based, returning -1 if all are blank
List.Transform(
List.RemoveFirstN( // list of column names except the first two
ColumnNames,
2
),
(columnName) => List.IsEmpty(List.RemoveNulls(Table.Column(myTable, columnName)))
),
false
), // Power query is lazy, so this won't actually look at every column, it will stop when it finds the first column!
ColumnNamesToKeep = List.RemoveFirstN(
ColumnNames,
ColumnsToRemove
),
ReturnTable = if (ColumnsToRemove = 1) then
"All columns were blank!" // PositionOf returned -1!
else
Table.SelectColumns(Source, ColumnsToKeep)
in
ReturnTable
You can now use this in other queries or you can load it to your spreadsheet. Unfortunately power query doesn't refresh live, you have to either explicitly refresh the query or use the "Refresh All" button in the data ribbon.
(I stressed the word "like" because I didn't debug. May contain syntax errors or other issues for you to debug.)
Im a new user in Power BI and im having a lot of trouble trying to convert my formulas in excel to work in Power BI.
My main issue is figuring out how to convert IF(AND(OR and several IF's. I have several formulas similar to this one so with the help of converting this one I could most likely do the rest based off this one.
Parameters would be a table in power bi.
E2, Q2, $B$2, etc. would be columns/fields in different tables in power bi.
=IF(E2="";"";IF(AND(OR(E2="Critical";E2="Maximum";E2="Urgent");Q2*24>Parameters!$B$2);"NO";
IF(AND(OR(E2="Alta";E2="High");Q2*24>Parameters!$B$3);"NO";
IF(AND(OR(E2="Media";E2="Medium");Q2*24>Parameters!$B$4);"NO";
IF(AND(OR(E2="Baja";E2="Low");Q2*24>Parameters!$B$5);"NO";
"YES")))))
This formula would be in a new column inside one of the tables.
You already did the work, it's very very similar, I've just added a new column and modify your formula like this:
Column = IF(Table1[E2]="","",
IF(AND(OR(Table1[E2]="Critical",OR(Table1[E2]="Maximum",Table1[E2]="Urgent")),Table1[Q2]*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Alta",Table1[E2]="High"),Table1[Q2]*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Media",Table1[E2]="Medium"),Table1[Q2]*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Baja",Table1[E2]="Low"),Table1[Q2]*24>Table1[B2]),"No","YES"
)
)
)
)
)
Where Table1 is my table, E2,Q2 and B2 are my columns name. About the "Q2*24", because you look like to use always Q2 and not Q2,Q3,Q4,... you can use one VAR instead of a full column with always the same value, like this:
Column =
VAR ReplaceQ2 = 10
RETURN
IF(Table1[E2]="","",
IF(AND(OR(Table1[E2]="Critical",OR(Table1[E2]="Maximum",Table1[E2]="Urgent")),ReplaceQ2*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Alta",Table1[E2]="High"),ReplaceQ2*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Media",Table1[E2]="Medium"),ReplaceQ2*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Baja",Table1[E2]="Low"),ReplaceQ2*24>Table1[B2]),"No","YES"
)
)
)
)
)
Hope this help
=IF(AND(LEFT(L6,3)="CGK",LEFT(M6,3)="CGK"),"Intracity",IF(AND(LEFT(N6,3)="CTC",LEFT(L6,3)=LEFT(M6,3)),"Intracity",IF(AND(LEFT(L6,3)=LEFT(M6,3),LEFT(N6,3)<>"CTC"),"Intercity","Domestik")))
Hi I am trying to change to write VBA for excel to clean up data elements that has extra information without impacting the other elements.
I am writing VBA for the first time my table is in the middle of the sheet.
Given Table and Requested Output.
I think your question was not clear in regard to the "steps" that you want to perform on your data (i.e. the exact logic or transformation that needs to be applied).
Based purely on your images and your comment, I make the "steps" to be:
Split any customer IDs in column valueC into multiple rows.
If column valueC does not contain customer IDs (i.e. is blank or contains non-customer ID text), leave it untouched.
My answer uses Power Query instead of VBA. If you are interested in trying it out, in Excel try clicking Data > Get Data > From Other Sources > Blank Query, then click Advanced Editor near the top-left, copy-paste the code below, then click Done.
You might need to change the name of the table in the first line of the code (below), as it was "Table1" for me, but I imagine yours is named something else. Also, the code below is case-sensitive. So if there is no column named exactly valueC, then you will get an error.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
fxProcessSomeText = (textToProcess as any) =>
let
canBeSplit = Text.StartsWith(textToProcess, "### customer id"),
result = if textToProcess is null then null else if canBeSplit then Text.Split(Text.BetweenDelimiters(textToProcess, "### customer id", " ###"), ",") else {textToProcess}
in
result,
invokeFunction = Table.TransformColumns(Source, {{"valueC", fxProcessSomeText}}),
expanded = Table.ExpandListColumn(invokeFunction, "valueC"),
reindex =
let
removeIndex = Table.RemoveColumns(expanded, {"index"}),
addIndex = Table.AddIndexColumn(removeIndex, "index", 1, 1),
moveIndex = Table.ReorderColumns(addIndex, List.Distinct(List.InsertRange(Table.ColumnNames(addIndex), 0, {"index"})))
in
moveIndex
in
reindex
My output table contains more rows than yours. Also, the value in column valueA, row 11 is 1415 for me (it is 1234 in your request output). Not sure if this is a mistake in your example, or if I'm missing some logic.
We have a spreadsheet that gets updated monthly, which queries some data from our server.
The query url looks like this:
http://example.com/?2016-01-31
The returned data is in a json format, like below:
{"CID":"1160","date":"2016-01-31","rate":{"USD":1.22}}
We only need the value of 1.22 from the above and I can get that inserted into the worksheet with no problem.
My questions:
1. How to use a cell value [contain the date] to pass the date parameter [2016-01-31] in the query and displays the result in the cell next to it.
2. There's a long list of dates in a column, can this query be filled down automatically per each date?
3. When I load the query result to the worksheet, it always load in pairs. [taking up two cells, one says "Value", the other contains the value which is "1.22" in my case]. Ideally I would only need "1.22", not the title, can this be removed? [Del won't work, will give you a "Column 1" instead, or you have to hide the entire row which will mess up with the layout].
I know this is a lot to ask but I've tried a lot of search and reading in the last few days and I have to say the M language beats me.
Thanks in advance.
Convert your Web.Contents() request into a function:
let
myFunct = ( param as date ) => let
x = Web.Contents(.... & Date.ToText(date) & ....)
in
x
in
myFunct
Reference your data request function from a new query, include any transformations you need (in this case JSON.Document, table expansions, remove extraneous data. Feel free to delete all the extra data here, including columns that just contain the label 'value'.
(assuming your table of domain values already exists) add a custom column like
=Expand(myFunct( [someparameter] ))
edit: got home and got into my bookmarks. Here is a more detailed reference for what you are looking to do: http://datachix.com/2014/05/22/power-query-functions-some-scenarios/
For a table - Add column where you get data and parse JSON
let
tt=#table(
{"date"},{
{"2017-01-01"},
{"2017-01-02"},
{"2017-01-03"}
}),
add_col = Table.AddColumn(tt, "USD", each Json.Document(Web.Contents("http://example.com/?date="&[date]))[rate][USD])
in
add_col
If you need only one value
Json.Document(Web.Contents("http://example.com/?date="&YOUR_DATE_STRING))[rate][USD]
Problem
I have two queries, one contains product data (data_query), the other (recode_query) contains product names from within the data_query and assigns them specific id_tags. id_tags are also column names within the data_query.
What I need to achieve and fail at
I need the data_query to look at the id_tag of the specific product name within the data_query, as parsed from the recode_query (this is already working and in place) and input the retrieved value within the specific custom column cell. In Excel, I would be using INDEX/MATCH combo:
{=INDEX(data_query[#Data];; MATCH(data_query[#id_tag]; data_query[#Headers]; 0))}
I have searched near and far, but I probably can't even spot the solution, even if I have come across it, as I am not that deep in the data manipulation and power query myself.
Is this what you're wanting?
let
DataQuery = Table.FromColumns({{1,2,3}, {"Boxed", "Bagged", "Rubberbanded"}}, {"ID","Pkg"}),
RecodeQuery = Table.FromColumns({{"Squirt Gun", "Coffee Maker", "Trenching Tool"}, {1,2,3}}, {"Prod Name", "ID2"}),
Rzlt = Table.Join(DataQuery, "ID", RecodeQuery, "ID2", JoinKind.Inner)
in
Rzlt