Good morning,
I have the following table with RAW data:
USER | YEAR | I1 | G1 | I2 | G2 | I3 | G3
A | 2021 | 1 | 3 | 4 | 2 | 5 | 1
B | 2021 | 3 | 2 | 1 | 2 | 4 | 1
And I need to create from this table another table, generating 3 new lines per Table A line.
USER | YEAR | # | I | G
A | 2021 | 1 | 1 | 3
A | 2021 | 2 | 4 | 2
A | 2021 | 3 | 5 | 1
B | 2021 | 1 | 3 | 2
B | 2021 | 2 | 1 | 2
B | 2021 | 3 | 4 | 1
I cannot find how to do it in PowerBI. I worked with other languages in which I would face this by extracting 3 times the information from the first table (in the first extraction I would select the I1 and G1, in the second one the I2 and G2 and in the third one the I3 and G3) and I would append it. However, I am not able to find in google how to do this with DAX.
Can anyone help me?
Thank you so much,
I believe you are asking how to do this with DAX simply because this is the only way you've heard. In Power BI, you should do such transformation using M, i.e. in Power Query Editor.
The format of the data is not optimal, so you will need to transform it a bit more. So open Power Query Editor by clicking on Transform data button in the ribbon, and then make a copy of your table by right-clicking it in the list and select Duplicate. Delete I1, I2 and I3 columns from one of the tables, and delete G1, G2 and G3 columns from the other. In each of the tables, select all three Ix/Gx columns and click Transform -> Unpivot Columns. After this step, the tables should looks like this:
Rename Values columns to I and G, and in each of the tables split the Attribute column by right clicking the column title and selecting Split column -> By number of characters... like this:
Rename the column with the numeric value (Attribute.2) to # in both tables and remove the other one (Attribute.1). After this stage, you should have two tables with the first 3 columns the same, and I and G columns containing the numeric values:
Now you must merge these two tables together, by clicking the drop down of Home -> Merge Queries and selecting Merge Queries as New, then select your two tables and select USER, YEAR and # columns as keys in both lists:
Expand the table by clicking the button in the columns title:
And leave only the other value, so at the end (after renaming the columns) you will get the desired result:
Related
I have table in RDS which consists two columns id and user activity at some time exactly values active/away.I get user activity every day so I need to add user activity column every day to that table.Any ideas how to do it?Now I have table with first two columns in RDS,but I am in stuck with how to add columns to that table
+-------------+------------+------------+
| id | 2020-08-13 | 2020-08-14 |
-----------------------------------------
| 12345 | active | away |
You could use an alter table ... add column, but this is not the right way to solve the problem.
In a relational database, you add additional rows for repeated data, not additional columns. So your table should look like this:
+-------------+-------------+------------+
| id | status_date | status |
------------------------------------------
| 12345 | 2020-08-13 | active |
| 12345 | 2020-08-14 | away |
Then you add a new row using an insert.
will be very grateful if you could share your experience and advice on the following problem in Power BI:
3 Tables given in the data model:
calendar dimension table
fact table on sessions
fact table on spending
| CW | Total cost | Sessions | Expected Column 1 | Expected Column 2 |
+----+-------------+-----------+-------------------+-------------------+
| 1 | 1200 | 50 | | |
| 2 | 1500 | 60 | 1200 | 50 |
| 3 | 1700 | 48 | 1500 | 60 |
| 4 | 1150 | 36 | 1700 | 48 |
| 5 | 900 | 29 | 1150 | 36 |
+----+-------------+-----------+-------------------+-------------------+
CW column indicates the calendar week and it is from calendar table. Sessions and Total cost are from sessions and spending tables respectively. Data is aggregated and visualized on calendar week level.
Problem: I need to create measures to derive Expected column 1 and expected column 2 based on total cost and sessions columns. Basically getting next values for each row similar to lead window function.
I have checked power BI community and there are several ideas (for example here https://community.powerbi.com/t5/Desktop/DAX-Query-to-Find-Next-Value/td-p/833896).
But these solution assume all columns are from the same table, however in the above described case
all 3 columns are from different tables.
Will the be possible to get expected columns 1 and 2 and how? Many thanks in advance!
I have the following tables:
Table A:
___________________
| ID | NUMBER |
|__________|________|
| 10000378 | 1 |
| | |
Table B:
_________________________________________________
| ... | ID | Column 1 | Column 2 |
|____________|__________|____________|____________|
| ... | 10000378 | 7 | 2 |
| | | | |
Table A and Table B share the same ID value.
Not that it matters too much, the ID in Table A is it's key, and can be found in Table B.
Table A's NUMBER column tells me which Table B column to read.
For example:
IF
[Table A].[NUMBER] = 1
THEN
put the value of [Table B].[Column 1] into [Table B].[My Column]
... and so on
As a visual example (my desired result):
_______________________________________________________________
| ... | ID | Column 1 | Column 2 | My Column |
|____________|__________|____________|____________|_____________|
| ... | 10000378 | 7 | 2 | 7 |
| | | | | |
With My Column being the result I am after (which comes from Column 1 - as specified by Table A's NUMBER value for the ID of 10000378).
What I have tried so far
I have set up a 1-to-1 relationship between Table A and Table B (on the ID column`).
I then went into the Power Query Editor tried adding a Conditional Column:
But that doesn't allow me to reference Table A in any way.
So I decided to try the Append Queries as New option:
Selecting the Two tables radio button, setting Primary Table to Table B and Table to append to the primary table to Table B.
With the idea of creating the following structure from which I can get my desired result (but adding the NUMBER column, which I don't really want but can live with):
_____________________________________________________________________________
| ... | ID | Column 1 | Column 2 | NUMBER | My Column |
|____________|__________|____________|____________|_____________|_____________|
| ... | 10000378 | 7 | 2 | 1 | 7 |
| | | | | | |
Doing the append copied all of the contents of Table B and inserted the NUMBER column which is fine, but all the values in that NUMBER column are now null.
I don't understand why this is the case.
How can I achieve my desired result?
Appending queries is used when you have additional rows of data that you’d like to add to an existing query (i.e. union/union all in SQL). Joining tables in Power BI is called "merging":
You can find more details in Combine queries section of Tutorial: Shape and combine data in Power BI Desktop article.
Note that if you have a proper relationship defined between your tables in the model (also described in the article above).
To do this, first merge Table B with Table A and expand the columns you'd like to add to the table (Column 1 and Column 2).
Once you have that, then write your custom column, My Column:
if [NUMBER] = 1 then [Column 1]
else if [NUMBER] = 2 then [Column 2]
else <whatever you want to return if not 1 or 2>
My data looks like this :
Item | Packaged Date | Delivery Date | Days took
1 | 17-05-2019 | 19-05-2019 | 2
2 | 23-05-2019 | 24-05-2019 | 1
3 | 22-05-2019 | 30-05-2019 | 8
I want to make a table using DAX where i have two columns
Number of Days | Items
0-5 | 2
5-10 | 1
This basically means within 5 days, 2 items in total were sold
and within 5 or 5-10 days , 1 item was sold
I found a way using DAX expression to solve the my own Question.
I created a DAX Query :
AggregatedDays = IF(Dates[Days]<=5 && Dates[Days]>=0 , "0-5 Days","5-10 Days")
A new table is created using Aggregated Days column and Items with items being "sum" from the "VALUES" table.
I've got data across 2 tables that kind of looks like:
Table 1
Mean | Activity | name_id
---------------------------
1 | Swimming | 1
3 | Basketball | 2
3 | Swimming | 3
9 | Running | 1
5 | Basketball | 3
TypeName | Name_id
------------------
ABC | 1
DEF | 2
GHI | 3
Assuming joins are in place, I want to run the equivalent of this SQL:
select activity, avg(mean)
from table1 a
inner join table2
on table1.name_id = table2.name_id
where table2.name_id = 'DEF'
I got a basic quick measure that does everything except for the filter for 'DEF' name_ids:
Average =
AVERAGEX(
KEEPFILTERS(VALUES('Table1'[Activity])),
CALCULATE(SUM('Table1'[Mean]))
)
I'm stumped as to where exactly I should put a filter. I also tried using CALCULATE as the base function, but was stumped on how to properly run that.