Forum Discussion
PowerBI calculation using column values separated by 7 days
- 6 years ago
A couple comments:
Your example talks about 7 days prior but it actually shows 6 days prior.
Are you sure you want logarithm dualis?
Did you mean columns when you wrote columns?
Here's the approach according to your example.
LogN table in Power Query:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZFLDoMwDESvUrGuwJ/Y8ZwFcf9rNJUawEAWXVjK4j05nlnXyRZfhISm9/Rq4/X3OGZ7f6maqBhQkSgMKJypSgOKKWE8wjhhMsLkjMXtXprFLDRApRuajA7ut9OsZiXIzbpRng0chpbCIEU37NHYM2kGqzuVuhv+bPBxB5ThWr0bqTrckmo7hCFB2O+IRyOOX7EDYkTcjVQp9Po9mgkubMp9h6R22+qbweJgYZZu8N+G/G2kzlGvETSDggy1tHS3Dw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #" " = _t, NewObs = _t, #" .1" = _t, #" Obs7DaysPrior " = _t, #" .2" = _t, #"LOG((NewObs/Obs7DaysPrior),2)" = _t]), #"Removed Other Columns" = Table.SelectColumns(Source,{"Date", "NewObs"}), #"Changed Type" = Table.TransformColumnTypes(#"Removed Other Columns",{{"Date", type date}, {"NewObs", type number}}) in #"Changed Type"Calculated Columns (!)
Obs7DaysPrior = var d = LogN[Date] return CALCULATE(sum(LogN[NewObs]),all(LogN),LogN[Date]=d-6) LogN = LOG(divide(LogN[NewObs],LogN[Obs7DaysPrior]),2)And the result
A couple comments:
Your example talks about 7 days prior but it actually shows 6 days prior.
Are you sure you want logarithm dualis?
Did you mean columns when you wrote columns?
Here's the approach according to your example.
LogN table in Power Query:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZFLDoMwDESvUrGuwJ/Y8ZwFcf9rNJUawEAWXVjK4j05nlnXyRZfhISm9/Rq4/X3OGZ7f6maqBhQkSgMKJypSgOKKWE8wjhhMsLkjMXtXprFLDRApRuajA7ut9OsZiXIzbpRng0chpbCIEU37NHYM2kGqzuVuhv+bPBxB5ThWr0bqTrckmo7hCFB2O+IRyOOX7EDYkTcjVQp9Po9mgkubMp9h6R22+qbweJgYZZu8N+G/G2kzlGvETSDggy1tHS3Dw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #" " = _t, NewObs = _t, #" .1" = _t, #" Obs7DaysPrior " = _t, #" .2" = _t, #"LOG((NewObs/Obs7DaysPrior),2)" = _t]),
#"Removed Other Columns" = Table.SelectColumns(Source,{"Date", "NewObs"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Other Columns",{{"Date", type date}, {"NewObs", type number}})
in
#"Changed Type"
Calculated Columns (!)
Obs7DaysPrior =
var d = LogN[Date]
return CALCULATE(sum(LogN[NewObs]),all(LogN),LogN[Date]=d-6)
LogN = LOG(divide(LogN[NewObs],LogN[Obs7DaysPrior]),2)
And the result
Thanks for responding! The 7 days is just a placeholder as I indicated in my request. Feel free to use 6 if you'd like.
Yes, I'd like to use log(base 2) for the calculations.
I'm not sure about your question: "Did you mean columns when you wrote columns?" but yes, the four columns presented in the table. The first two are the available data and the last two are what I would like to calculate.
Could you help me walk through your LogN PowerQuery? It's completely new to me. Specifically, I'd like to understand how I can incorporate these pieces of code into my workbook:
Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZFLDoMwDESvUrGuwJ/Y8ZwFcf9rNJUawEAWXVjK4j05nlnXyRZfhISm9/Rq4/X3OGZ7f6maqBhQkSgMKJypSgOKKWE8wjhhMsLkjMXtXprFLDRApRuajA7ut9OsZiXIzbpRng0chpbCIEU37NHYM2kGqzuVuhv+bPBxB5ThWr0bqTrckmo7hCFB2O+IRyOOX7EDYkTcjVQp9Po9mgkubMp9h6R22+qbweJgYZZu8N+G/G2kzlGvETSDggy1tHS3Dw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true])and:
in type table [Date = _t, #" " = _t, NewObs = _t, #" .1" = _t, #" Obs7DaysPrior " = _t, #" .2" = _t, #"LOG((NewObs/Obs7DaysPrior),2)" = _t]),
Finally, is there a function similar to Excel's "IFERROR" to get rid of the "infinity" values"?
Thank you!
- lbendlin6 years agoSuper User
The Power Query code is to be taken as is, and put into the advanced editor of a blank query, replacing whatever boilerplate code is there.
The first two lines of that are basically the data and schema of your sample table.
Power BI has a concept of "calculated columns" and "measures" - those are two very different things with different computations. You chose columns, that's what the formulas serve up.
The DIVIDE() code allows for alternative results to avoid dividing by zero, and DAX also has IF(ISBLANK()) patterns to help you decide what to do in case nothing is returned.
- Anonymous6 years agoNot applicable
Thank you. So the PowerQuery has created a table called "LogN" with only one column called "LogN", which stores only one text value, which is the PowerQuery code itself.
Not sure if this was intended.
- lbendlin6 years agoSuper User
No. Not like this.
Create a blank query. Right click it. Select Advanced Editor. Replace the existing code with the one from my post.