Add column with hardcoded values in PowerBI - powerbi

It's probably extremely simple but I can't find an answer. I have created a new column and I would like to use the DAX syntax to fill the column with hardcoded values.
I can write this: Column = 10 and I will get a column of 10s but let's say my table has 3 rows and I would like to insert a column with [10, 17, 155]. How can I do that?

Try using DATATABLE function
Table = DATATABLE("Column Name",INTEGER,{{10},{17},{155}})
You can also put more columns with their own data if you want to, check this
https://learn.microsoft.com/en-us/dax/datatable-function

Assuming your table has a primary key column, say, ID, you could create a new table with just the column you want to manually input.
ID Value
---------
1 10
2 17
3 155
You can create this table either through the Enter Data button or create it using the DAX DATATABLE function as #Deltapimol suggests.
Once you have this table you can create a relationship to your existing table in the data model at which point you can either use this new table in your report to get the values you need or if you really need them in the existing table for some reason, you can pull them over using the RELATED function in a calculated column.
Table1 = GENERATESERIES(1, 3)
Table2 = DATATABLE(
"ID", INTEGER,
"Value" INTEGER,
{{1, 10},{2, 17},{3, 155}}
)
Now you can create a relationship from Table1 to Table2[ID] and then define a calculated column on Table1 as follows:
ValueFromTable2 = RELATED(Table2[Value])
If you don't want to create a relationship, then you could use the LOOKUPVALUE function instead in a calculated column on Table11.

Related

PowerBI Creating New Table using values in other tables

I have multiple data tables in a Power BI report and am hoping to create a new summarized table with new column and selected columns and rows from other data tables. Each data tables are quite large with different data elements so it's difficult to append the tables together to form one big table. Below is a simplified version just to illustrate the issue and what i wanted to achieve:
ID 1 Name is Orange
ID 2 Name is Apple
The last column: Sum Value (Agg.) in each table are Measures.
The result table I am trying to get is like below:
So basically I wanted to use Table 1 and Table 2 to create a new summarized Table with an added column 'Item Name' replacing the 'ID' column and the 'Sum Value' column. So far I've tried the Summarize and AddColumn function but couldn't get Measure Sum Value to the table.
If anyone can help, that'd be great!!
Thankyou
Michelle
This reshaping should really be done in PQ but if you insist on DAX, here you go.
Table =
VAR a = ADDCOLUMNS(
SUMMARIZE(Orange, Orange[Sum Value (Agg.)]),
"Item Name",
"Orange"
)
VAR b = ADDCOLUMNS(
SUMMARIZE(Apple, Apple[Sum Value (Agg.)]),
"Item Name",
"Apple"
)
RETURN UNION(a,b)

Redshift add new column based on values from existing column

I have a Redshift table I want to alter adding a new column, which values are derived from an existing column on the table.
Basically, only adding a column "year" which extracts the year from the column "snapshot_date".
Any ideas how to achieve that? Tried following code, but it errors out.
ALTER TABLE test_schema.table_name ADD year AS ( extract(year from snapshot_date) );

I can't sort a table column by a calculated column

Trying to sort a column in my custom date table (a csv file) via a calculated column in the same table but am seeing an error. The calculated column does not reference the column I wish to sort by. Here's the DAX for the calculated column:
PeriodOffset =
Dates[Period] + Dates[FiscalYear] * 13
- CALCULATE ( VALUES ( Dates[Period] ), Dates[Date] = TODAY () )
- CALCULATE ( VALUES ( Dates[FiscalYear] ), Dates[Date] = TODAY () ) * 13
My date table has every date from 2003/4 to 2034/35, along with custom period numbers, calendar and fiscal years etc. The column I am trying to sort is called PeriodFiscalYear. Each value in that column has only one entry in the PeriodOffset column so it's not that.
The weird thing is, I have had this working in a previous report. In this instance, I was simply trying to recreate the functionality but it won't do it. Even stranger, if I create the PeriodFiscalYear column as a calculated column (currently it's hard-coded in the csv file), it works! So I have a sort-of workaround, I would just like to understand what is going on.
Thanks
I believe this has to do with the fact that data column are sorted when data are ingested into PBI. Calculated columns are calculated only at a later time.
Therefore:
you can sort data column only with other data columns (because calculated columns have not been calculated yet)
you can sort calculated column with both data column and calculated column
Solution:
A) PeriodFiscalYear becomes a calculated column
B) PeriodOffset becomes a data column (either in your CSV or Power Query)
I actually figured this out. The problem was with my data model - I had a circular relationship in there as I was deriving the Period column in one table using my calendar table then linking them back in the relationship!
I created a linking table with the keys in both to make the relationship, then hid it.
Thanks

Calculated Column to get average from values in another table in PowerBI

This might be very basic but I am new to PowerBI.
How do I get average of values for unique ID into another table.
For eg. My Table 1 has multiple ID values. I have created another table for unique ID which I am planning to used to join other table.
I want a calculated column in table 2 which will give me average value of respective ID from table 1.
How do I get the calculated column like shown below
In stead of creating a new table with the averages per ID and then joining on that, you could also do it directly with a calculated column using the following DAX expression:
Average by ID = CALCULATE(AVERAGE('Table 1'[Values]),ALLEXCEPT('Table 1','Table 1'[ID]))
Not exactly what you asked for, but maybe it's useful anyway.
How's it going?
The quickest way I can think of doing this would be to use
SUMMARIZECOLUMNS
You can accomplish this by creating another table based on your initial fact table like so:
Table 2 =
SUMMARIZECOLUMNS ( 'Table 1'[ID], "Avg", AVERAGE ( 'Table 1'[Values] ) )
Once this table has been created, you can create a relationship.
This will work in either SSAS or in PowerBI directly.
Hope this helps!! Have a good one!!

How to add rows to a new custom table based on values in other table?

I need to add duplicate entries in a new custom table from the inputs given in another table.
e.g. consider a table with columns
Subscriber
Effective_date
Expiriation_date
Service_Code
Now i want to dynamically create a custom table which will have duplicate entries with the same
Subscriber
Effective_date
Expiriation_date
Service_Code
I also need new columns for the dates between "Effective_date" & "Expiriation_date", so I could filter and see how many months a customer was subscribed for until leaving.
Right now I have this:
but instead of 3 - which is the number of month until the customer left, I want to see the months between:
May
June
July
how can I do this?
If I understand what you are asking for, this should give you what you want.
First, you will need a table full of dates. If you don't already have one, you can have PowerBI create one for you by clicking on 'Modeling' -> 'New Table' and then entering in this formula (you can alter the dates to expand or shrink the dates included in the table).
Dates = CALENDAR(DATE(2016, 1, 1), DATE(2017, 12, 31))
Then create a new table with this formula. This will give you a new table with rows for each date between the 'Effective_Date' and 'Expiration_Date' from the original table.
ServicePeriodExpanded = FILTER(CROSSJOIN(ServicePeriod, 'Dates'), ServicePeriod[Effective_Date] <= 'Dates'[Date] && IF(ServicePeriod[Expiration_Date] = BLANK(), TODAY(), ServicePeriod[Expiration_Date]) >= 'Dates'[Date])
Here are the results.