Solved

Creating a new column

  • 7 September 2021
  • 4 replies
  • 34 views

Is there a way to create a new column based on the values of another column? In another viz tool, I could use the following --- Case [column_name] when ‘Levi’ then ‘Apple’ else [column_name] END

They were called calculated columns

icon

Best answer by Eric_Lyons 7 September 2021, 21:37

Hi @3piecechickendinner

 

We would nest conditions or even use an AND or OR condition. 

 

 

Thanks,

Eric

View original

4 replies

Userlevel 4
Badge

Hi @3piecechickendinner 

 

You can use a LookML Case statement or SQL Case WHEN in the LookML to define a new field. This article outlines both structures. Or, with a custom field, you can use the new case function outlined here

 


SQL CASE
dimension: semester {
sql: CASE
WHEN ${TABLE}.status = 0 THEN 'Fall'
WHEN ${TABLE}.status = 1 THEN 'Winter'
WHEN ${TABLE}.status = 2 THEN 'Spring'
ELSE 'Summer'
END ;;
}



LookML CASe

dimension: semester {
case: {
when: {
sql: ${TABLE}.status = 0 ;;
label: "Fall"
}
when: {
sql: ${TABLE}.status = 1 ;;
label: "Winter"
}
when: {
sql: ${TABLE}.status = 2 ;;
label: "Spring"
}
when: {
sql: ${TABLE}.status = 3 ;;
label: "Summer"
}
}
}

Thanks,

Eric

Hi @3piecechickendinner 

 

You can use a LookML Case statement or SQL Case WHEN in the LookML to define a new field. This article outlines both structures. Or, with a custom field, you can use the new case function outlined here

 


SQL CASE
dimension: semester {
sql: CASE
WHEN ${TABLE}.status = 0 THEN 'Fall'
WHEN ${TABLE}.status = 1 THEN 'Winter'
WHEN ${TABLE}.status = 2 THEN 'Spring'
ELSE 'Summer'
END ;;
}



LookML CASe

dimension: semester {
case: {
when: {
sql: ${TABLE}.status = 0 ;;
label: "Fall"
}
when: {
sql: ${TABLE}.status = 1 ;;
label: "Winter"
}
when: {
sql: ${TABLE}.status = 2 ;;
label: "Spring"
}
when: {
sql: ${TABLE}.status = 3 ;;
label: "Summer"
}
}
}

Thanks,

Eric

Thank you, but is there a way to do it in Table Calculation? I’ve used this--if(${product_viewed.brand}="Levi's","Barbie",${product_viewed.brand})--but not sure how to accomplish this for multiple conditions

Userlevel 4
Badge

Hi @3piecechickendinner

 

We would nest conditions or even use an AND or OR condition. 

 

 

Thanks,

Eric

Userlevel 3

Hello,

I’m adding another way if you are running the latest version of the Looker software:

https://docs.looker.com/exploring-data/adding-fields/custom-measure#custom_grouping

I found this new feature pretty cool and far more intuitive to use than nested if table calculations :)

 

Antho

Reply