Forum Discussion
Retrieve data from Google Analytics by calculating a dimension
- Anonymous2 years ago
Hi Giuseppe89
If you don't want to import your data.
You can use the REST API in Power Query, which is a viable way to do it at the moment:Google Analytics Data API | Google for Developers
You can use the API to get data from data source and write mcode to get the output data you want.
Here is a sample for your reference, and this code needs to be modified:
let Source = Web.Page(Web.Contents("YOUR_API_ENDPOINT", [Headers=[Authorization="Bearer YOUR_ACCESS_TOKEN"]])), Data = Source{0}[content], JsonData = Json.Document(Data), #"Converted to Table" = Record.ToTable(JsonData), #"Added Custom" = Table.AddColumn(#"Converted to Table", "Category", each if Text.Contains([PagePath], "category_a") then "A" else if Text.Contains([PagePath], "category_b") then "B" else "Other"), #"Grouped Rows" = Table.Group(#"Added Custom", {"YearMonth", "Category"}, {{"TotalUsers", each List.Sum([Users]), type number}}) in #"Grouped Rows"Here for your reference:
The above is an implementation that requires you to have a deep understanding of Power Query M code and understand the relevant knowledge of API.
Best Regards
Zhengdong Xu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi Giuseppe89
If you don't want to import your data.
You can use the REST API in Power Query, which is a viable way to do it at the moment:
Google Analytics Data API | Google for Developers
You can use the API to get data from data source and write mcode to get the output data you want.
Here is a sample for your reference, and this code needs to be modified:
let
Source = Web.Page(Web.Contents("YOUR_API_ENDPOINT", [Headers=[Authorization="Bearer YOUR_ACCESS_TOKEN"]])),
Data = Source{0}[content],
JsonData = Json.Document(Data),
#"Converted to Table" = Record.ToTable(JsonData),
#"Added Custom" = Table.AddColumn(#"Converted to Table", "Category", each if Text.Contains([PagePath], "category_a") then "A" else if Text.Contains([PagePath], "category_b") then "B" else "Other"),
#"Grouped Rows" = Table.Group(#"Added Custom", {"YearMonth", "Category"}, {{"TotalUsers", each List.Sum([Users]), type number}})
in
#"Grouped Rows"
Here for your reference:
The above is an implementation that requires you to have a deep understanding of Power Query M code and understand the relevant knowledge of API.
Best Regards
Zhengdong Xu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.