Forum Discussion
Fregglino
1 year agoNew Member
Contents from different tables in one column
Hi, I have the following problem and have been trying to solve it for a long time.
I hope you can help me.
I have 2 tables in my Power BI project (simplified):
Table 1 (sales date, product, costs):
| Date | Product | Costs |
| 25.03.2022 | Name 1 | 523 |
| 26.08.2022 | Name 2 | 471 |
| 21.5.2023 | Name 1 | 362 |
22.6.2023 | Name 3 | 215 |
5.1.2024 | Name 2 | 853 |
2.8.2024 | Name 3 | 235 |
21.1.2025 | Name 2 | 856 |
Table 2 (year, product, sales target):
| Year | Product | Sales target for year |
| 2022 | Name 1 | 1000 |
| 2022 | Name 2 | 2000 |
| 2022 | Name 3 | 1500 |
| 2023 | Name 1 | 1000 |
| 2023 | Name 2 | 2000 |
| 2023 | Name 3 | 1500 |
| 2024 | Name 1 | 1000 |
| 2024 | Name 2 | 2000 |
| 2024 | Name 3 | 1500 |
| 2025 | Name 1 | 1000 |
| 2025 | Name 2 | 2000 |
| 2025 | Name 3 | 1500 |
I would like to create the following visual:
| Year | Product | Total and objective mixed |
| 2022 | Name 1 | Total cost of product 1 |
| 2022 | Name 2 | Sales Target from Table 2 (Copy) |
| 2022 | Name 3 | Sales Target from Table 2 (Copy) |
| 2023 | Name 1 | Total cost of product 1 |
| 2023 | Name 2 | Sales Target from Table 2 (Copy) |
| 2023 | Name 3 | Sales Target from Table 2 (Copy) |
| 2024 | Name 1 | Total cost of product 1 |
| 2024 | Name 2 | Sales Target from Table 2 (Copy) |
| 2024 | Name 3 | Sales Target from Table 2 (Copy) |
| 2025 | Name 1 | Total cost of product 1 |
| 2025 | Name 2 | Sales Target from Table 2 (Copy) |
| 2025 | Name 3 | Sales Target from Table 2 (Copy) |
1 Reply
- AnonymousNot applicable
Hi Fregglino ,
You can use power query to ahcieve this, Click Transform Data and open power queryCreate a blank query and open advanced editor
Paste the following code
let Source1 = #"Table 1", Source2 = #"Table 2", #"Extracted Year" = Table.AddColumn(Source1, "Year", each Date.Year([Date]), Int64.Type), #"Grouped Rows" = Table.Group(#"Extracted Year", {"Year", "Product"}, {{"Total Costs", each List.Sum([Costs]), type number}}), #"Merged Queries" = Table.NestedJoin(Source2, {"Year", "Product"}, #"Grouped Rows", {"Year", "Product"}, "Grouped Rows", JoinKind.LeftOuter), #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Total Costs"}, {"Total Costs"}), #"Replaced Sales Target" = Table.ReplaceValue(#"Expanded Grouped Rows", each [#"Sales target for year"], each if [Product] = "Name 1" then [Total Costs] else [#"Sales target for year"], Replacer.ReplaceValue, {"Sales target for year"}), #"Renamed Columns" = Table.RenameColumns(#"Replaced Sales Target", {{"Sales target for year", "Total and objective mixed"}}), #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Total Costs"}), #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Total and objective mixed", type number}}) in #"Changed Type"Final output
Best regards,
Albert HeIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly