Is there SQL function to update table by replacing one column with data (DIFFERENT TYPE) from another column (in a related table)? - sql-update

I run into a problem in SQL, I don't know how to update table A by replacing its column "Edu" with another column "Level" of table B.
Two tables have a relationship in column "Edu"
Different data types bt "Edu" (number) & "Level" (Short Text)
Can anyone explain why the below code does not work? And kindly suggest a solution. Thanks!
UPDATE
A
SET
A.Edu= CAST(B.Level AS Varchar(Max))
FROM
A INNER JOIN B ON A.Edu=B.Edu;

Maybe an idea.
I think your query doesnt work because you try to update a key using a the same time this key, A.Edu.
My solution :
create in the table A a column EduBis with same value as Edu.
Then update with A.EduBis=B.Edu in you clause in the join.

Related

Clean up a dimension table

My dimension tables contains more rows than my fact table, I would like my dim table fields to show only the values in the fact table when used as a filter in the filter panel.What cleaning/modeling steps are the best to achieve this.
I also know how to write sql if that is an option for an answer.
Rather than use a table for your dimension, use a view that has an inner join to the fact table

Can we compare two tables in a Switch Case in DAX Power BI?

Is it possible to include columns from 2 different tables in DAX Switch Function ?
Both of my tables are linked by Many to Many Relationship.
I am trying to create a calculated column in Table A where I want to compare Columns from Table B, but I can't add the Table B column in the DAX Switch formula.
Any suggestions would be appreciated.
Thanks & Regards,
Pratik
Sample
Table B
Table A & Expected Result
In my opinion, it doesn't matter whether this table has many relationship, the only thing matter is that you are calculated based on your original table instead of calculate on a new measure, then result is unpredictable:
By create new table from the original table, and add column for the comment, I can provide result as your expectation, hope it solve your problem and accept the answer :)
Create new table from original table (Sheet1 is my old table) and select on ID column
Table = SELECTCOLUMNS(Sheet1,"ID",Sheet1[ID])
Create new column for comment
Column = SWITCH(LOOKUPVALUE('Sheet1'[Value],Sheet1[ID],'Table'[ID]), 0,"Matched with A","Matched with B")
The outcome for the new table:
Instead of trying to use two columns from two different tables for comparison in DAX, you can use the 'RELATED` DAX function to import a column from Table A to Table B or vice-versa. Then you can use this column for comparison in DAX.
Just set the appropriate relationship between Table A and Table B and create a new column with the RELATED DAX function to import the table that you want to use in the SWITCH function.

Power BI / Power Query - M language - playing with data inside group table

Hello M language masters!
I have a question about working with grouped rows when the Power Query creates a table with data. But maybe it is better to start from the beginning.
Important information! I will be asking for example only about adding an index. I know that there are different possibilities to reach such a result. But for this question, I need an answer about the possibility to work on tables. I want to use this answer in different actions (e.g table sorting, adding columns in group table).
In my sample data source, I have a list of fake transactions. I want to add an index for each Salesman, to count operations for each of them.
Sample Data
So I just added this file as a data source in Power BI. In Power query, I have grouped rows according to name. This step created form me column contained a table for each Salesman, which stores all his or her operations.
Grouping result
And now, I want to add an index column in each table. I know, that this is possible by adding a new column to the main table, which will be store a new table with added index:
Custom column function
And in each table, I have Indexed. That is good. But I have an extra column now (one with the table without index, and one with a table with index).
Result - a little bit messy
So I want to ask if there is any possibility to add such an index directly to the table in column Operations, without creating the additional column. My approach seems to be a bit messy and I want to find something cleaner. Does anyone know a smart solution for that?
Thank you in advance.
Artur
Sure, you may do it inside Table.Group function:
= Table.Group(Source, {"Salesman"}, {"Operations", each Table.AddIndexColumn(_, "i", 1, 1)})
P.S. To add existing index column to nested table use this code:
= Table.ReplaceValue(PreviousStep,each [index],0,(a,b,c)=>Table.AddColumn(a,"index", each b),{"Operations"})

How to create table based on minimum date from other table in DAX?

I want to create a second table from the first table using filters with dates and other variables as follows. How can I create this?
Following is the expected table and original table,
Go to Edit Queries. Lets say our base table is named RawData. Add a blank query and use this expression to copy your RawData table:
=RawData
The new table will be RawDataGrouped. Now select the new table and go to Home > Group By and use the following settings:
The result will be the following table. Note that I didnt use the exactly values you used to keep this sample at a miminum effort:
You also can now create a relationship between this two tables (by the Index column) to use cross filtering between them.
You could show the grouped data and use the relationship to display the RawDate in a subreport (or custom tooltip) for example.
I assume you are looking for a calculated table. Below is the workaround for the same,
In Query Editor you can create a duplicate table of the existing (Original) table and select the Date Filter -> Is Earliest option by clicking right corner of the Date column in new duplicate table. Now your table should contain only the rows which are having minimum date for the column.
Note: This table is dynamic and will give subsequent results based on data changes in the original table, but you to have refresh both the table.
Original Table:
Desired Table:
When I have added new column into it, post to refreshing dataset I have got below result (This implies, it is doing recalculation based on each data change in the original source)
New data entry:
Output:

Select Columns from multiple tables - Django

I am trying to select all the columns from one table and only a single column from the second table using the Django ORM.The two tables are related by a foreign key but the column in the second table which i want, is not related, so using select_related doesn't work.
I used
FirstTable.objects.filter().values("All first table columns separated by a comma","second tablename__column name")
and this worked fine. Since, the columns are very large in number, i do not want to list all of them in "values". So, i am looking for an easy way to do this.
Thanks for your help
EDIT:
Thanks for the help! I was able to get it working by using annotate like the following.
FirstTable.objects.filter().annotate(variable_name=F("SecondtableFieldName"))
You Can use the extra method in this.
FirstTable.objects.filter().extra("write down here what every yo want")