Split row in multiple other rows in Power Bi based on a division of a number - powerbi

In Power BI Desktop i have a table from an excel file and i want to split a row based on a division between the value of a specific column and a default number.
In more details lets assume tha we have a table like this :
if the default value we want to devide column Amount is 50,then the desirable result would be something like that :
Do you have any idea how can i implement that in Power query editor or with dax?
Thanks

Tested this in Power Query for Excel, but hopefully should work for you in Power BI too. If you create a function like:
divisionToList = (numberToDivide as number, numberToDivideBy as number) as list =>
let
divisionResult = numberToDivide / numberToDivideBy,
isResultValid = (divisionResult >= 0) and (Number.Mod(divisionResult, 1) = 0),
errorIfInvalid = Error.Record("Cannot create a list with " & Text.From(divisionResult) & " items", Number.ToText(numberToDivide) & " / " & Number.ToText(numberToDivideBy) & " = " & Text.From(divisionResult), null),
listOrError = if isResultValid then List.Repeat({divisionResult}, divisionResult) else error errorIfInvalid
in listOrError,
It should divide two numbers and return a list of length d in which each element is d (d is the result of the division). This list can then, in the context of a table, be expanded into new rows.
There is some basic error handling in the function for cases where the division yields a problematic number (since you can't have a list with, for example, 5.1 elements or -1 elements). You can change/remove this handling if necessary.
I think this code below takes me from your first image to your second image -- and hopefully will give you some idea on how to go about achieving this.
let
mockData = Table.FromColumns({{200, 400}, {"A", "B"}}, type table [Amount = number, Description = text]),
defaultValue = 50, // Not sure what logic is required for arriving at this figure, so have simply assigned it.
divisionToList = (numberToDivide as number, numberToDivideBy as number) as list =>
let
divisionResult = numberToDivide / numberToDivideBy,
isResultValid = (divisionResult >= 0) and (Number.Mod(divisionResult, 1) = 0),
errorIfInvalid = Error.Record("Cannot create a list with " & Text.From(divisionResult) & " items", Number.ToText(numberToDivide) & " / " & Number.ToText(numberToDivideBy) & " = " & Text.From(divisionResult), null),
listOrError = if isResultValid then List.Repeat({divisionResult}, divisionResult) else error errorIfInvalid
in listOrError,
invokeFunction = Table.TransformColumns(mockData, {{"Amount", each divisionToList(_, defaultValue), type list}}),
expanded = Table.ExpandListColumn(invokeFunction, "Amount")
in
expanded

Related

Lookup multiple columns in Google Sheets and return a list from the matches

I think the title accurately describes what I'm trying to achieve.
https://docs.google.com/spreadsheets/d/1sRQzXXZ4a3vjAwvsH4rrWfijxV4jCl_OV3iQO00yvlQ/edit?usp=sharing
Essentially, I have a table of data for houses, the street it's on, whether it has a pool or gates etc. and I'm trying to create a lookup in Google Sheets so if someone is trying to find a house with a pool for a maximum of $800k then I can return results that match the criteria.
This is how the table data looks.
I want to be able to query the data here in columns D, E, F, G (G being a maximum value in the lookup) and return the data in columns A, B, C if everything matches.
I would enter on a different tab, the maximum budget (which would need to do a max lookup of column G, and then look for any Y/N in the other columns and return a list of all matches.
Is this possible with Google Sheets?
Thanks, for any help you can offer.
use:
=QUERY(Houses!A:I,
"select C,B,A,H
where H <= "&B3&"
and D = '"&B4&"'
and E = '"&B5&"'
and F = '"&B6&"'", 0)
update:
=IFERROR(QUERY(HousingData,
"select C,B,A,G
where G <= "&B3&
IF(B4="Y", " and D = '"&B4&"'", )&
IF(B5="Y", " and E = '"&B5&"'", )&
IF(B6="Y", " and F = '"&B6&"'", )&
IF(B7="Y", " and J = '"&B7&"'", ), 0), "No houses found.")

generate a one-column table that contains hundreds of different categories using M or DAX

I need to split my products into a total of 120 predefined price clusters/buckets. These clusters can overlap and look somewhat like that:
As I dont want to write down all of these strings manually: Is there a convenient way to do this in M or DAX directly using a bit of code?
Thanks in advance!
Dave
With m-Query you can create a function. Open the query editor. Richt click and create empty query. Create function (ignore warning) and call it : RowGenerator.
Open advanced editor and past the following code:
let
Bron = (base as number, start as number, end as number) => let
Bron = Table.FromList(List.Generate(() => start, each _ <= end, each _ + 1), Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Aangepaste kolom toegevoegd" = Table.AddColumn(Bron, "Aangepast", each Number.ToText(base) & " - " & Number.ToText([Column1]))
in
#"Aangepaste kolom toegevoegd"
in
Bron
This function creates a table where base is your first number and start, end the range.
Add another empty query, open the advanged editor and paste:
let
Bron = List.Generate(() => 0, each _ < 5, each _ + 1),
#"Geconverteerd naar tabel" = Table.FromList(Bron, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Aangeroepen aangepaste functie" = Table.AddColumn(#"Geconverteerd naar tabel", "test", each RowGenerator(_[Column1], _[Column1] + 1, 5)),
#"test uitgevouwen" = Table.ExpandTableColumn(#"Aangeroepen aangepaste functie", "test", {"Column1", "Aangepast"}, {"Column1.1", "Price Cluster"}),
#"Kolommen verwijderd" = Table.RemoveColumns(#"test uitgevouwen",{"Column1", "Column1.1"})
in
#"Kolommen verwijderd"
This creates first a list of 5 rows, then it calls the previous made function for each row and the last step is to expend the rows and remove the not needed columns.
Enjoy:
You can create this bucket by DAX (New Table):
Table = SELECTCOLUMNS(
GENERATE(SELECTCOLUMNS(GENERATESERIES(0,10,1),"FirstPart",[Value]), SELECTCOLUMNS(GENERATESERIES(0,10,1),"SecondPart",[Value]))
,"Bucket", [FirstPart] & " - " & [SecondPart]
)
Table = SELECTCOLUMNS(
GENERATE(SELECTCOLUMNS(GENERATESERIES(0,9,1),"FirstPart",[Value]), TOPN([FirstPart], SELECTCOLUMNS(GENERATESERIES(1,9,1),"SecondPart",[Value]), [SecondPart],ASC))
,"Bucket", [FirstPart] & " - " & [SecondPart]
)

Filter formula for multiple cell references

How can I make it so the formula on E2 reads values entered in any of the search cells and displays it as results, considering I have a button to clear all search boxes and users are instructed to only search one box at a time and to press the button if multiple boxes are filled?
See image
Here's my editable Spreadsheet
Much appreciated.
try:
=IFNA(QUERY(A2:C4; "where A = '"&B6&"'
or B = '"&B7&"'
or C = '"&B8&"'"; 0))
I'd like results to be shown only if the data users look for are in the same row.
=IFNA(QUERY(A2:C4; "where A = '"&B6&"'
and B = '"&B7&"'
and C = '"&B8&"'"; 0))
UPDATE:
=IFNA(QUERY(A2:C4; "where "&TEXTJOIN(" and "; 1;
IF(B6="";;"A = '"&B6&"'");
IF(B7="";;"B = '"&B7&"'");
IF(B8="";;"C = '"&B8&"'"))&""; 0))
You can combine conditions via + and *
=IFNA(
FILTER(A2:C4;(A2:A4=B6)+(B2:B4=B7)+(C2:C4=B8));
"Enter the data for the request"
)

Power BI, Power Query - Go trough list and add text with each step (for each)

I have a list for example:
A
B
C
D
Now I want to create a query in power query that will create a text like this:
"The letters are: " A B C D
I already have this query:
let
xx= KEYListfunction(),
list = Text.Split(xx, ","),
text = "",
loop = List.Accumulate(list, 0, (state, current) => text = text & " " & current ))
in
loop
But the result only says "FALSE"
Are you looking for this?
let
list = {"A".."D"},
text = "The letters are: " & Text.Combine(list, ", ")
in
text

Stuck adding formula to dynamically added columns

I have a query that returns a dynamic number of columns. I need to dynamically add the same amount of custom columns. I have successfully gotten this far. I'm stuck creating the formulas for the custom columns. This is what I have so far. (This is not the actual query, this is simplified)
Here is the Code:
Test = List.Accumulate(MyList, Source,
(state, current) => Table.AddColumn(
state, "A Temp" & Number.ToText(current), each [A1])
)
For now, I just added [A1] as a place holder for the formula. I need the formula to accumulate as follows:
A Temp1 = [A1] / [TOTAL]
A Temp2 = [A2] / [TOTAL]
A Temp3 = [A3] / [TOTAL]
Above is not actual code. Just what I need the formulas to do for each custom column.
Is this possible? I have tried everything I could think of. I'm using power query in excel BTW.
This isn't exactly what you asked for, but I think it will help.
Test = List.Accumulate(
List.Select(Table.ColumnNames(Source), each _ <> "TOTAL"),
Source,
(state, current) => Table.AddColumn(state,
"Temp " & current,
each Record.Field(_, current) / [TOTAL]))
It's not exactly what you asked for as it gives column names like Temp A1 instead of A Temp1.