Add column from one table to another - powerbi

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>

Related

PowerBI generate table from another table

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:

Add column to existing table in rds

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.

Power BI - Filtered count not grouping by values in table

I have two tables (subject & category) that are both related to the same parent table (main). Because of the foreign key constraints, it looks like Power BI automatically created the links.
Simple mock-up of table links
I need to count the subjects by type for each possible distance range. I tried a simple calculation shown below for each distance category.
less than 2m =
CALCULATE(
COUNTA('Category'[Descr]),
'Subject'[Distance] IN { "less than 2m" }
)
However, the filter doesn't seem to apply properly.
I want...
+------+--------------+--------------+--+
| Descr| less than 2m | more than 2m | |
+------+--------------+--------------+--+
| Car | 2 | 1 | |
| Sign | 4 | 2 | |
+------+--------------+--------------+--+
but I'm getting...
+------+--------------+--------------+--+
| Descr| less than 2m | more than 2m | |
+------+--------------+--------------+--+
| Car | 3 | 3 | |
| Sign | 6 | 6 | |
+------+--------------+--------------+--+
It's just giving me the total count by type which is correct but isn't applying the filter by distance so I can break it down.
I'm sure this is probably really simple but I'm pretty new with DAX and I can't figure this one out.
I wish I could mark Kosuke's comment as an answer. The issue was indeed with having to enable cross-filtering. This can either be done clicking on the link on your model or using a function to temporarily enable the cross filter.

Sum where version is highest by another variable (no max version in the whole data)

I'm struggling having this measure to work.
I would like to have a measure that will sum the Value only for the max version of each house.
So following this example table:
|---------------------|------------------|------------------|
| House_Id | Version_Id | Value |
|---------------------|------------------|------------------|
| 1 | 1 | 1000 |
|---------------------|------------------|------------------|
| 1 | 2 | 2000 |
|---------------------|------------------|------------------|
| 2 | 1 | 3000 |
|---------------------|------------------|------------------|
| 3 | 1 | 5000 |
|---------------------|------------------|------------------|
The result of this measure should be: 10.000 because the house_id 1 version 1 is ignored as there's another version higher.
By House_id the result should be:
|---------------------|------------------|
| House_Id | Value |
|---------------------|------------------|
| 1 | 2000 |
|---------------------|------------------|
| 1 | 3000 |
|---------------------|------------------|
| 2 | 5000 |
|---------------------|------------------|
Can anyone help me?
EDIT:
Given the correct answer #RADO gave, now I want to further enhance this measure:
Now, my main Data table in reality has more columns.
What if I want to add this measure to a table visual that splits the measure by another column from (or related to) the Data table.
For example (simplified data table):
|---------------------|------------------|------------------|------------------|
| House_Id | Version_Id | Color_Id | Value |
|---------------------|------------------|------------------|------------------|
| 1 | 1 | 1 (Green) | 1000 |
|---------------------|------------------|------------------|------------------|
| 1 | 2 | 2 (Red) | 2000 |
|---------------------|------------------|------------------|------------------|
| 2 | 1 | 1 (Green) | 3000 |
|---------------------|------------------|------------------|------------------|
| 3 | 1 | 1 (Green) | 5000 |
|---------------------|------------------|------------------|------------------|
There's a Color_Id in the main table that is connected to a Color table.
Then I add a visual table with ColorName (from the ColorTable) and the measure (ColorId 1 is Green, 2 is Red).
With the given answer the result is wrong when filtered by ColorName. Although the Total row is indeed correct:
|---------------------|------------------|
| ColorName | Value |
|---------------------|------------------|
| Green | 9000 |
|---------------------|------------------|
| Red | 2000 |
|---------------------|------------------|
| Total | 10000 |
|---------------------|------------------|
This result is wrong per ColorName as 9000 + 2000 is 11000 and not 10000.
The measure should ignore the rows with an old version. In the example before this is the row for House_Id 1 and Color_Id Green because the version is old (there's a newer version for that House_Id).
So:
How can I address this situation?
What If I want to filter by another column from (or related to) the Data table such as Location_Id? It is posible to define the measure in such a way that could work for any given number splits for columns in the main Data table?
I use "Data" as a name of your table.
Sum of Latest Values =
VAR Latest_Versions =
SUMMARIZE ( Data, Data[House_id], "Latest_Version", MAX ( Data[Version_Id] ) )
VAR Latest_Values =
TREATAS ( Latest_Versions, Data[House_id], Data[Version_Id] )
VAR Result =
CALCULATE ( SUM ( Data[Value] ), Latest_Values )
RETURN Result
Measure output:
How it works:
We calculate a virtual table of house_ids and their max versions, and store it in a variable "Latest_Versions"
We use the table from the first step to filter data for the latest versions only, and establish proper data lineage
(https://www.sqlbi.com/articles/understanding-data-lineage-in-dax/)
We calculate the sum of latest values by filtering data for the latest values only.
You can learn more about this pattern here:
https://www.sqlbi.com/articles/propagate-filters-using-treatas-in-dax/

Update Autonumber field after DELETE query

INTRODUCTION AND RELEVANT INFORMATION:
I have MS ACCESS 2007 database that I edit using ADO and C++.
PROBLEM:
My problem is that primary key also represents an ordinal number of the record, and after deletion, it should be properly updated. Primary key is of autonumber type.
Here is an example of what I am talking about:
| #PK | Other data ... |
| 1 | ... |
| 2 | ... |
| 3 | ... |
| 4 | ... |
| 5 | ... |
Now if I delete the 3rd record I get the following problem:
| #PK | Other data ... |
| 1 | ... |
| 2 | ... |
| 4 | ... |
| 5 | ... |
but I should get the following result:
| #PK | Other data ... |
| 1 | ... |
| 2 | ... |
| 3 | ... | // edited to reflect the change ( previous value was 4 )
| 4 | ... | // edited to reflect the change ( previous value was 5 )
If I delete last record and then insert new one I get this result:
| #PK | Other data ... |
| 1 | ... |
| 2 | ... |
| 3 | ... |
| 4 | ... |
| 6 | ... | // this should be 5
QUESTIONS:
Is there a way for me to programmatically update the autonumber field after I perform the DELETE query ?
EDIT:
Since I am aware this is a bad practice, I would prefer adding new field that should be ordinal number so my table can look like this:
| #PK | Ordinal | Other data ... |
| 1 | 1 | ... |
| 2 | 2 | ... |
| 4 | 3 | ... |
| 5 | 4 | ... |
but I would prefer it to update itself automatically. If this is not possible, I would prefer to update the field with SQL query after I perform the deletion.
Thank you.
Best regards.
It is possible, but not the right way. Primary keys are used for relationships, so if you change the values, you need to update all related tables. Even if you currently don't have any related tables, you still should consider adding a separate field for the order, otherwise you may face the same problem in the future when you want to add related tables.
EDIT To answer your question:
Is there a way to add another field that would represent ordinal number and will automatically increment after inserting new record?
If you set it to autonumber, it will automatically increment, but you will not be able to modify it. You can set it to number and when you insert, you use SELECT MAX(oredinal) + 1 FROM mytable to increment it.
For MS Access use
ALter Table Customer alter column CustomerID Counter(1,1)
For Sql Server
DBCC CHECKIDENT (orders, RESEED, 0)
This will set the value of the next ID to be 1, you can use above command.
Ref URL# http://www.howtogeek.com/howto/database/reset-identity-column-value-in-sql-server/
I have decided to add a new field in my table that will hold the ordinal number of the record.
If we assume the field is named OrdinalNumber then the following solution worked for me:
// when inserting record, I just had to add COUNT( PK ) + 1
INSERT INTO MyTable ( OrdinalNumber , ... ) SELECT COUNT( PK ) + 1 , ...
from MyTable ;
// when deleting, I had to perform following two queries :
DELETE from MyTable where PK = ? ;
// decrement all the successors ordinal number by one
UPDATE MyTable set OrdinalNumber = ( OrdinalNumber - 1 ) where ( PK > ? );
Everything seem to work well. I wish there was an easier way though...
Thanks everyone for helping. I have upvoted all the answers.