Forum Discussion

BIXL's avatar
BIXL
Icon for Resolver I rankResolver I
10 years ago

create new table with partial columns from another table in dax

Hi

I wonder if there is a way to create a table in dax that is the same like another table but with less colmun.

for example :

table "A" has "col1","col2","col3"

I want to create table "B" with just "col1","col2"

 

thanks

17 Replies

  • SqlJason's avatar
    SqlJason
    Icon for Memorable Member rankMemorable Member

    In Power BI Desktop, you can create a new calculated table with the formula like shown below

     

    NewTableName = SUMMARIZE(ExistingTableName, ExistingTableName[column1], ExistingTableName[column2])

    • KiDix's avatar
      KiDix
      Frequent Visitor

      IS it possible to add a where clause to this as well??

  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    You could also copy the query that creates your 1st table, edit the query and remove the offending column from the query.

    • k3rz0rg's avatar
      k3rz0rg
      Icon for Advocate III rankAdvocate III

      Greg_Deckler I usually follow this steps; try not to bother with dax regarding minor stuffs; like duplicate table, drop the unnecessary column, if required, remove duplicates, then if required, add the relationship manually. Yet just wanted to refresh my memory with dax expressions... :smileyvery-happy:

  •  I am not a pro power bi user. I think I find out an easy way to solve your problem if we are facing the same problem. 

    TABLE B = 

    SELECTCOLUMNS(CALCULATETABLE( TABLEA,FILTER(TABLEA,TABLEA[col1]=x)),"col1",'TABLEA'[col1],"col2",TABLEA[col2])
     
    Consider the duplicate rows, you can wrap a "DISTINCT" on the formula as below"
     

    TABLE B = 

    DISTINCT(SELECTCOLUMNS(CALCULATETABLE( TABLEA,FILTER(TABLEA,TABLEA[col1]=x)),"col1",'TABLEA'[col1],"col2",TABLEA[col2]))
     
    • hello_MTC's avatar
      hello_MTC
      Icon for Helper III rankHelper III

      what is that "x", make it simple bro! I didn't understand your dax function.

  • Hi, the following should work when creating a new table:

    tableB = ALL(tableA[col1],tableA[col2])

  • greggyb's avatar
    greggyb
    Icon for Resident Rockstar rankResident Rockstar

    Any reason you have to do this in DAX?

     

    There's not really a good way to do this in the general case. If 'Table'[Col1] and 'Table'[Col2] have no duplicates, you can use one of SUMMARIZE() as shown above or GROUPBY() with identical syntax, but both of these perform a logical group by operation. This means that if you have duplicates, your table will have fewer rows AND fewer columnns than the base.

     

    As Greg_Deckler, mentioned, duplicating the query in Power Query is likely a better option.

      • agustin_garcia's avatar
        agustin_garcia
        Frequent Visitor

        I have a table with N Columns where I have (among others) the following columns and conditions
        A) There are 3 executing/managing sites (IN-HOUSE)
        B) There are 4 outsourcing sites (OFFSHORE)
        C) For every offshore Project, there must always be an IN-HOUSEManaging site

        1) DEPARTMENT - Contains Coding/Managing site (Either in-house or Offshore) - IN-HOUSE_A,IN-HOUSE_B, IN-HOUSE_C, OFFSHORE_A, OFFSHORE_B, OFFSHORE_C, OFFSHORE_D
        2) Project_Name
        3) Plan_Date

        a) If the project is being Managed and Coded 100% in-house in one SINGLE SITE, then there is just ONE record for that PROJECT in that DEPARTMENT in a certain PLAN_DATE, e.g.: DEPARTMENT = IN-HOUSE_A - (B or C)
        b) If the project is being Managed and Coded 100% in-house in more than one Executing Site, then there is ONE record for each Executing Site in the DEPARTMENT column for that PROJECT in a certain PLAN_DATE (in this case, it could be considered as separate projects since Budgeting is done at the Executing/Managing Site level)
        c) If a Project is sent offshore (it can be sent to 1 or more off-shore Coding sites), there would be two or more records
        i. One record with DEPARTMENT = "IN-HOUSE_A" -(B or C)
        ii. One record with DEPARTMENT = "OFFSHORE_A" -(B, C or D) for each off-shore site where code is being done

        I want to find a way in DAX (or Power Query) to add a CUSTOM COLUMN in that table to indicate the Managing Site for each outsourced project for easier reporting and filtering, e.g.:
        a) Cost plan by Executing site for 100% managed projects
        b) Cost Plan by Coding site for outsourced projects
        c) Cost Plan by Managing Site-Coding Site

        NOTES:
        To detect Managing Site, I was thinking on locate DUPLICATES (PROJECT_DATE + PLAN_DATE) then:
        i. locate the DEPARTMENT value for record containing DEPARTMENT = IN-HOUSE_X (A, B, or C) value
        ii. Locate it's associated records (with same PROJECT_DATE + PLAN_DATE) but DEPARTMENT = OFFSHORE_X (A, B, or C) and copy the IN-HOUSE department from the previous record to the Custom column, understanding that can be 1 or more offshore records||
        iii. For unique records, I'd add a custom label (e.g.: "100% IN-HOUSE") or leave it blank in that Custom column

        I can't change the design on the table since it comes from a C.A. Clarity PPM feed

  • JohnDawson's avatar
    JohnDawson
    Frequent Visitor

    I see what SqlJason is saying but it seems a bit crazy to bring in two tables by query twice, once with one set of columns and once with another, call the second import of the tables SelectedColumns1 and SelectedColumns2

     

    In my case I wanted to use EXCEPT to find rows in SelectedColumns1 not in SelectedColumns2; this requires the two tables have a) exactly the same number and type of columns, and b) the right columns to produce the required result i.e. no identity type columns

     

    What I'd really like to be able to do is to use EXCEPT and create my SelectedColumns1 and SelectedColumns2 on the fly, as table expressions, which doesn't seem to be possible

     

    In my case using CALCULATETABLE will remove some rows, as pointed out by greggyb

     

    I can't do what Greg_Deckler says either as if you copy the table you get all columns and there's no way to hid columns, or rather if you do hide them, it affects only the reporting not the matching - I think

    • Greg_Deckler's avatar
      Greg_Deckler
      Icon for Community Champion rankCommunity Champion

      JohnDawson - Don't copy the table, copy the query. Edit that query to remove the columns that you do not want. You now have 2 queries that create 2 tables, one with all the columns and one with fewer than all of the columns.

      • Anonymous's avatar
        Anonymous
        Not applicable

        Greg_Deckler , thanks for this elegant and simple solution. 

  • This solution does not work with sharepoint lists. It works with SQL, though.