Create Do While loop in DAX - powerbi

I need to create something like this using DAX. Preferably highlighted columns as two columns of a table. I have managed to set the preview of the requirement But I'm unable to proceed with it. Hope someone can help me with this.
I have tried with DAX queries, but it won't give me what I need to be done.

This feels more like a Power Query to me. Try pasting this into a blank query in the Advanced Editor:
let
StartingValue = 8,
Deduction = 0.10,
StartDate = #date(2023,2,14),
Times=6,
#"Multiplier" = 1 - Deduction,
Source = Table.FromList({1..Times}, Splitter.SplitByNothing(), type table [Index=Int64.Type], null, ExtraValues.Error),
#"Add Amount" = Table.AddColumn(Source, "Amount", each StartingValue * Number.Power(#"Multiplier",[Index]-1), type number),
#"Add Date" = Table.AddColumn(#"Add Amount", "Date", each Date.AddMonths(StartDate,[Index]-1), type date)
in
#"Add Date"
You can change the 4 input values into Parameters and make this a function in Power Query to easily create new tables.

Related

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}}))

Create idex with repeated data

I have data from an external source that is downloaded in csv format. This data shows the interactions from several users and doesn't have an id column. The problem I'm having is that I'm not able to use index because multiple entries represent interactions and processes. The interactions would be the group of processes a specific user do and the process represents each actions taken in a specific interaction. Any user could repeat the same interaction at any time of day. The data looks likes this:
User1 has 2 processes but there were 3 interactions. How can I assign an ID for each interaction having into consideration that there might be multiple processes for a single user in the same day. I tried grouping them in Power Query but it groups the overall processes and I'm not able to distinguish the number of interactions. Is it better to do it in Dax?
Edit:
I notice that it is hard to understand what I need but I think this would be a better way to see it:
Process 2 are the steps done in an interaction. Like in the column in yellow I need to add an ID taking in to consideration where an interaction start and where it ends.
I'm not exactly sure I follow what you describe. It looks to me like user1 has 4 interactions--Processes AA, AB, BA, and BB--but you say 3.
Still, I decided to take a shot at providing an answer anyway. I started with a CSV file set up like you show.
Then brought the CSV into Power Query and, just to add a future point of reference so that you could follow the Id assignments better, I added an index column that I called startingIndex.
Then I added a custom column combining the processes that I understand actually define an interaction.
Then I grouped everything by users and Interactions into a column named allData.
Then I added a custom column to copy the column that was created from the earlier grouping, to sort the tables within it, and to add an index to each table within it. This essentially indexed each user's interaction group. (Because all of your interactions occur on the same date(s), the sorting doesn't help much. But I did it to show where you could do it if you included datetime info instead of just a date.)
Then I added a custom column to copy the column that was created earlier to add the interactions index, and to add an Id item within each table within it. I constructed each Id by combining the user, interactions, and interactionIndex for each.
Then I selected the latest column I had created (complexId) and removed all other columns.
Last, I expanded all tables without including the Interactions and Index columns. (The Index column was the index used for the interactions within the groups and no longer needed.) I included the startingIndex column just so you could see where items originally were at the start, in comparison to their final Id.
Given your new example, to create the Interaction ID you show, you only need the first two columns of the table. If not part of the original data, you can easily generate the third column (Process2))
It appears you want to increment the interaction ID whenever the Process changes
Please read the comments in the M code and explore the Applied Steps to better understand the algorithm:
M Code
let
//be sure to change table name in next row to your real table name
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"User", type text},
{"Process", type text},
{"Process2", type text}
}),
//add an index column
idx = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
//Custom column returns the Index if
// the current Index is 0 (first row) or
// there has been no change in user or process comparing current/previous row
// else return null
#"Added Custom" = Table.AddColumn(idx, "Custom",
each if [Index]=0
then 0
else
if [Process] <> idx[Process]{[Index]-1} or [User] <> idx[User]{[Index]-1} then [Index]
else null),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
//Fill down the custom column
// now have same number for each interactive group
#"Filled Down" = Table.FillDown(#"Removed Columns",{"Custom"}),
//Group by the "filled down" custom column with no aggregation
#"Grouped Rows" = Table.Group(#"Filled Down", {"Custom"}, {
{"all", each _, type table [User=nullable text, Process=nullable text, Process2=nullable text, Custom=number]}
}),
//add a one-based Index column to the grouped table
#"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Interaction ID", 1, 1, Int64.Type),
#"Removed Columns1" = Table.RemoveColumns(#"Added Index",{"Custom"}),
//Re-expand the table
#"Expanded all" = Table.ExpandTableColumn(#"Removed Columns1", "all",
{"User", "Process", "Process2"}, {"User", "Process", "Process2"})
in
#"Expanded all"
Source
Results

Power bi and google analytics. How to set up filters for date before getting full data

How to set up filters for date before getting full data. Right now pbi export all data from day one. Is more than 1.7mil. rows. I reach limit after 1mil and way to use date filter inside pbi is not a option.
So are posible way setdate range parameter (example last 3 month) before export starts?
In adv editor source code:
let
Source = GoogleAnalytics.Accounts(),
#"1*******1" = Source{[Id="1*******1"]}[Data],
#"UA-1*******1-1" = #"1*******1"{[Id="UA-1*******1-1"]}[Data],
#"1*******6" = #"UA-1*******1-1"{[Id="1*******6"]}[Data],
#"Added Items" = Cube.Transform(#"1*******6",
{
{Cube.AddAndExpandDimensionColumn, "ga:eventAction", {"ga:eventAction"}, {"Event Action"}},
{Cube.AddAndExpandDimensionColumn, "ga:eventLabel", {"ga:eventLabel"}, {"Event Label"}},
/*{Cube.AddAndExpandDimensionColumn, "ga:date", {"ga:date"}, {"Date"}},*/
{Cube.AddMeasureColumn, "Unique Events", "ga:uniqueEvents"}
})
in
#"Added Items"
#"1*******1" = Source{[Id="1*******1"]}[Data],
#"UA-1*******1-1" = #"1*******1"{[Id="UA-1*******1-1"]}[Data],
#"1*******6" = #"UA-1*******1-1"{[Id="1*******6"]}[Data],
My guess, after source location we need somehow set date range parameters.
I know that it is an old question and supposed that you solve it.
I think it may be useful for those who google this question.
After step #"Added Items" replace text (in #"Added Items")
(be aware first in SelectRows( should be the name of the previous step)
with next
#"combinedData" = Table.Combine({
Table.SelectRows(#"Added Items", each Text.Contains([Month of Year], "2020"))
Table.SelectRows(#"Added Items", each Text.Contains([Month of Year], "2019"))
})
in
combinedData

Data profiling in Power BI

I want to profile every single data table I have in my Power BI report. By data profile I mean something like this:
Are there ways to make a data profile view in Power BI? DAX measure or calculated columns?
Alternatively, you can also recommend other data quality tools that can handle such tasks since I find it a bit difficult to achieve this result in Power BI.
Now I feel dumb after writing a manual query that did what it turns out Table.Profile does in one shot. However I will mention you can automatically get a profile for every table in your data set by using the #shared reference and filtering down to the tables:
let
Source = #shared,
#"Converted to Table" = Record.ToTable(Source),
#"Added Custom" = Table.AddColumn(#"Converted to Table", "TableCheck", each Type.Is(Value.Type([Value]), type table)),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([TableCheck] = true)),
#"Filtered Rows1" = Table.SelectRows(#"Filtered Rows", each [Name] <> "NAME_OF_THIS_QUERY"),
#"Added Custom1" = Table.AddColumn(#"Filtered Rows1", "Profile", each Table.Profile([Value])),
#"Expanded Profile" = Table.ExpandTableColumn(#"Added Custom1", "Profile", {"Column", "Min", "Max", "Average", "StandardDeviation", "Count", "NullCount", "DistinctCount"}, {"Profile.Column", "Profile.Min", "Profile.Max", "Profile.Average", "Profile.StandardDeviation", "Profile.Count", "Profile.NullCount", "Profile.DistinctCount"})
in
#"Expanded Profile"
And replace "NAME_OF_THIS_QUERY" with whatever you name the query so it doesn't try to profile itself.
Power BI has a built-in data profiler
Open Power BI and refer to the menu ribbon
Click Home
Click Edit Queries
Click View
Select Column Profile to view stats about your data
In the query editor, you can use the Table.Profile function on any table.
You can do multiple ones simultaneously like this:
= Table.Combine({Table.Profile(Table1),Table.Profile(Table2)})
Edit:
To see the profile, create a new Blank Query and define it as = Table.Profile(Table1). If you open the Advanced Editor, the M code looks like this:
let
Source = Table.Profile(Table1)
in
Source

Create Power BI Web Query based on data table

I am using Power BI Desktop to create a web query to the stack overflow API. I want to obtain the number of questions asked on stack overflow for each user which is extracted from a prepopulated table of users in Power BI.
So I want something that will look like this:
Pre-populated Users:
6231494
User2
User3
StackOverflow Questions:
6231494: 5
User2: 12
User3: 10
Here is my current code for my web query in the advanced editor:
let
Source = Json.Document(Web.Contents("http://api.stackexchange.com/2.2/users/6231494/answers?order=desc&sort=activity&site=stackoverflow")),
items = Source[items],
#"Converted to Table" = Table.FromList(items, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"owner", "is_accepted", "score", "last_activity_date", "creation_date", "answer_id", "question_id"}, {"Column1.owner", "Column1.is_accepted", "Column1.score", "Column1.last_activity_date", "Column1.creation_date", "Column1.answer_id", "Column1.question_id"}),
#"Expanded Column1.owner" = Table.ExpandRecordColumn(#"Expanded Column1", "Column1.owner", {"reputation", "user_id", "user_type", "profile_image", "display_name", "link"}, {"Column1.owner.reputation", "Column1.owner.user_id", "Column1.owner.user_type", "Column1.owner.profile_image", "Column1.owner.display_name", "Column1.owner.link"})
in
#"Expanded Column1.owner"
I basically need a way to set the current hardcoded ID (6231494) equal to the list of IDs from my predefined user table
Assume that you have a Users table with column ID as number:
You can make a few changes to your Questions query to turn it into a custom function in Power BI:
(id as number) =>
let
Source = Json.Document(Web.Contents("http://api.stackexchange.com/2.2/users/" & Text.From(id) & "/answers?order=desc&sort=activity&site=stackoverflow")),
items = Source[items]
...
(id as number) => is added to turn the query into a function;
and the user ID is replaced with Text.From(id).
Here I renamed it to GetQuestions to keep it clear that it's a function:
You can then add a custom column to invoke the function:
Expand it and you shall find the columns from the original query:
And you can work with the data to get the results you want (e.g. number of questions).