1

I have a function that takes some text input and I want to convert it to a query name. So source2 should be the query #"input text". How do i do this ? I have searched the web everywhere and cannot find the answer.

let
    fnGetLeague = (leagueName as text) =>
    let
        Source = #"Matches Today",
        Source2 = #"leagueName",
        join = Table.NestedJoin(Source, "homeTeam.team_id",Source2, "team_id", "leagueData" )
        
    in
        join
in
    fnGetLeague

my main goal is to join some data from a specific query (different leagues) inside my "Matches Today" Query.

In my "matches today" query i have a column called league, so i want to pass this in as a variable in my function, assign it to the correct query, and join some data from that specific query inside my matches today query.

enter image description here

enter image description here

In the end there will be many league queries, so dynamically pointing to the right query is my main goal.

i want to avoid using power pivot at this moment because it's so slow.

5
  • Hi. I don't think that is possible that way. What is your main goal, so I can see if I know any other way? Commented Jun 29, 2020 at 9:20
  • i tried to explain and edited my question, I hope it's clear because my English is not that perfect :) Commented Jun 29, 2020 at 9:49
  • @Dreekun Any idea ? Commented Jul 1, 2020 at 6:11
  • No, I don't know how to do that. To pass column cell values to a function I always use Add Custom Column. I don't know any other way, sorry Commented Jul 1, 2020 at 9:49
  • What i'm looking for basically is convert the input text to a table named #"input text" Commented Jul 1, 2020 at 11:41

2 Answers 2

2
+100

Answer to question-as-asked

To reference a query via the text of its name, requires using a fairly well-hidden intrinsic variable - #sections. You can read the documentation, but I personally found this Q&A about lists of queries more helpful. As noted in the Q&A, do be cautious about potential recursion issues when using #sections.

For your function in particular, changing Source2 as shown below should work. However, be aware that the function will error out if there are no queries with the specified name.

let
    fnGetLeague = (leagueName as text) =>
    let
        Source = #"Matches Today",
        Source2 = List.First(Table.SelectRows(Record.ToTable(#sections[Section1]), each [Name] = leagueName)[Value]),
        join = Table.NestedJoin(Source, "homeTeam.team_id",Source2, "team_id", "leagueData" )
        
    in
        join
in
    fnGetLeague

Note: This function will return a new query that does a left outer join from #"Matches Today" to the specified query. This will not alter #"Matches Today" in any way. If you want to add data from the league-specific queries directly to #"Matches Today" (instead of to a "copy"), then I would recommend one of the methods below.


Alternative

Change your function as shown below, then invoke the function through the table options in the #"Matches Today" query. This will add the data to the #"Matches Today" query instead of creating a new query.

let
    fnGetLeague = (leagueName as text, teamID as number) =>
    let
        Source = Record.ToTable(#sections[Section1]),
        LeagueTable = List.First(Table.SelectRows(Source, each [Name] = leagueName)[Value]),
        LeagueTeam = if Value.Is(LeagueTable, type table) then Table.SelectRows(LeagueTable, each [team_id] = teamID) else null
    
    in
        LeagueTeam
in
    fnGetLeague

Invoking a Custom Function


table options

enter image description here


Troubleshooting


Depending on the function results, you may not see the "table-column" options (Expand/Aggregate). If so, you can sometimes force it by adding , type table in the end of the Table.AddColumn() function (created when invoking custom function) as shown below.

enter image description here

enter image description here

If the expand/aggregate button is visible, but attempting to use it results in a

No columns were found

message, then there are two possible options:

  1. Manually add the appropriate expand/aggregate step.
  2. Create a "blank" table to be returned by the function instead of null

Manual steps


Expansion

Adding a manual expansion has a few, mostly simple, steps:

  1. Add the custom step. This can be accomplished be right-clicking the step you want the manual step to follow, then clicking the "Insert Step" option.

    Insert Step option

  2. The custom step will have the equation = #"Previous Step" (where #"Previous Step" will be the table from the previous step. If you added the custom step directly after the source, the equation will be = Source, etc.).

  3. Replace the existing equation in the custom step with the Table.ExpandTableColumn() equation (PQ Doc). For example, if I wanted to expand the columns "count" and "Price" from the existing table column "Data" (and assuming the previous step is "Grouped Data") then the expansion equation would look like

    = Table.ExpandTableColumn(#"Grouped Data", "Data", {"count", "Price"}) 
    

    and if I wanted "count" to be renamed to "units", the equation would be

    = Table.ExpandTableColumn(#"Grouped Data", "Data", {"count", "Price"}, {"units", "Price"})
    

Aggregation

For Aggregating, there are two options:

  1. Expanding the relevant columns, then add a grouping step
  2. Use the Table.AggregateTableColumn() equation (PQ Doc)

For example, finding max "Price" from column "Data" (previous step is "Grouped Data") would be:

= Table.AggregateTableColumn(#"Grouped Data", "Data", {{"Price", List.Max, "max of Price"}})

There are more examples in the documentation.


Blank table


This method may be best if the columns you're interested in won't be changing, but the potential aggregations or expansions might. i.e. if the format and column names of the league tables are mostly consistent between tables, and shouldn't be changing.

Instead of having the custom function return null, it should return a "blank" table - a table with the desired columns, but no rows. For example, if the "blank" table should have a "price" column and a "name" column, the code for the blank table would be:

#table(type table [#"price"=number, #"name"=text], {})

However, since expanding a column with an empty table results in a row of null values, it would be equivalent to use:

#table(type table [#"price"=number, #"name"=text], {{null, null}})

You could also choose to seed the table with a zero or an empty string:

#table(type table [#"price"=number, #"name"=text], {{0, ""}})

Below, I have a copy of the custom function returning a blank table with columns {price, name} instead of null in the LeagueTeam line of code:

let
    fnGetLeague = (leagueName as text, teamID as number) =>
    let
        Source = Record.ToTable(#sections[Section1]),
        LeagueTable = List.First(Table.SelectRows(Source, each [Name] = leagueName)[Value]),
        LeagueTeam = if Value.Is(LeagueTable, type table) then Table.SelectRows(LeagueTable, each [team_id] = teamID) else #table(type table [#"price"=number, #"name"=text], {})
    
    in
        LeagueTeam
in
    fnGetLeague
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thanks for your answer. I don't get it completely though. I use your alternative version and adjust the function, then i invoke a custom function. The result here gives null as well for tables which do not exist, correct ! When the table does exist, I can see the correct table linked. Is it possible to expand/aggregate the column even when there are null values ? I get the message No colums were found. And when exactly do i add the custom column with the equation ? Or is this the same as the custom function ?
@BjornMorrhaye Last Q 1st - adding the , type table into the step with the custom function makes the expand/aggregate option available if it wasn't already. Since it sounds like the option is available, but not finding the columns, there are two solutions: 1) Adding a custom step with the expansion/aggregation or 2) if all your league tables have the same columns (name & #), create a "blank" version for the function to use instead of null.
@BjornMorrhaye I'll try to add some info, when I have a few minutes, but if you have a preference, I can focus on that one first.
@BjornMorrhaye I've edited my answer to include detail on various options for dealing with it (in the Troubleshooting section).
0

I would build a query that appends all the data from all the leagues together, with each set of rows having a leagueName column filled with the appropriate value e.g "Greece - Super League 2".

Then I would add a Filter Rows step that references your leagueName parameter, to restrict the output to just the rows for that league.

1 Comment

Hi, thanks for your answer. This is a possible solution but i don't want to use another query that appends all data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.