Skip to content

Commit be72cc4

Browse files
committed
more documentation additions
1 parent 689cb53 commit be72cc4

File tree

5 files changed

+40
-26
lines changed

5 files changed

+40
-26
lines changed

docs/_quarto.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ quartodoc:
3737

3838
sections:
3939
- title: Power Bpy
40-
desc: Python functions for building Power BI dashboards
40+
desc: The first thing you should create is an instance of the Dashboard class. Then use the `add_page()` or `load_page()` functions to attach instances of the internal `_Page` class. All methods are attached to either dashboards or pages. Click below to see which methods are attached to which. For more details about how to build dashboards, see the test dashboard [tutorial](https://www.russellshean.com/powerbpy/example_dashboards/Test%20Dashboard/Testing%20Dashboard.html) section of this website.
4141
contents:
4242
# the functions being documented in the package.
4343
# you can refer to anything: class methods, modules, etc..

docs/example_dashboards/Sanky Chart and Table/chart_and_table.qmd

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
2-
Title: "WOW Power BI 2025 week 13 demonstration"
2+
title: "WOW Power BI 2025 week 13 demonstration"
33
date: 2025-12-11
44
description: Let's add a table and a sanky chart to a dashboard.
55
image: "https://github.com/Russell-Shean/powerbpy/raw/main/docs/assets/images/sanky_screenshot.png"
66
---
77

88
# Sanky Chart and Table
99
I used the PowerBpy python module to recreate an <a href="https://workout-wednesday.com/pbi-2025-w13/">sanky chart and table</a> dashboard from the <a href="https://workout-wednesday.com/">Workout Wednesday Group</a> challenge. Here's a screenshot of the original dashboard (top) and the dashboard I recreated using Power Bpy (bottom). (It appears that the data has changed since the original author created his dashboard, so my version and his look a bit different because the data is a bit different, but the overall format is pretty similiar). The rest of the blog will describe key parts of the code I used to make the dashboard. The full code is available [here](https://github.com/Russell-Shean/powerbpy-demos/tree/main/WOW/2025/13).
10-
![my sanky](https://github.com/Russell-Shean/powerbpy/raw/main/docs/assets/images/sanky_screenshot.png)
10+
<img id="my-sanky-img" src="https://github.com/Russell-Shean/powerbpy/raw/main/docs/assets/images/sanky_screenshot.png" style="margin-bottom:10px">
1111
![wow sanky](https://github.com/Russell-Shean/powerbpy/raw/main/docs/assets/images/WOW_sanky.png)
1212

1313
## Assumptions
@@ -82,8 +82,6 @@ store_names = (
8282
.reset_index(drop=True)
8383
)
8484

85-
86-
8785
# Makes sure OrderDate is a date
8886
sales["OrderDate"] = pd.to_datetime(sales["OrderDate"])
8987

@@ -120,8 +118,6 @@ sales_by_store_and_date = (
120118
# Define a default for if neither condition is matched
121119
default="middle_period"
122120

123-
124-
125121
)
126122
)
127123

@@ -134,17 +130,13 @@ sales_by_store_and_date = (
134130
.assign(
135131

136132
sales_size = lambda df: np.select(
137-
138-
139-
140133
[
141134
df["store_total_sales"] < 1000,
142135

143136
(df["store_total_sales"] >= 1000) &
144137
(df["store_total_sales"] < 5000),
145138

146139
df["store_total_sales"] >= 5000
147-
148140
],
149141

150142
[
@@ -153,20 +145,14 @@ sales_by_store_and_date = (
153145
"Large"
154146
],
155147
default="Unknown"
156-
157-
158148
)
159-
160-
161149
)
162150

163151
# Merge the store names onto the dataframe
164152
.merge(
165-
166153
store_names,
167154
on = "StoreKey",
168155
how = "left"
169-
170156
)
171157

172158
# pivot the dataframe to expand the time period and sales size columns wider
@@ -175,7 +161,6 @@ sales_by_store_and_date = (
175161
columns="time_period",
176162
values=['store_total_sales', "sales_size"]
177163
)
178-
179164
)
180165

181166

docs/example_dashboards/Test Dashboard/Testing Dashboard.qmd

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,38 @@ page1.add_button(label = "Move to page 2",
207207
```
208208
If the code works you should get two buttons that look like this. The first button should open a new www.google.com tab in your browser. The second button should send you to page 2.
209209

210-
![image](https://github.com/Russell-Shean/powerbpy/blob/main/docs/assets/images/buttons_demo.gif)
210+
![image](https://media.githubusercontent.com/media/Russell-Shean/powerbpy/refs/heads/main/docs/assets/images/buttons_demo.gif)
211211

212212
## Add a choropleth map
213213
Often we have data that is tied to geographic regions that can be respresented by a polygon such as states, counties, zip codes, or countries. A choropleth map uses color to represent values in the data by shading the polygons according to the value of the data. (If that was way too abstract, see the example below. Basically we're creating a color code map to represent the data.)
214214

215-
This function does a lot under the hood so you don't have to. It creates measures in the dataset so that bins will automatically adjust when filtering conditions change, it creates the map and adds text boxes for a legend and and a slicer to allow the user to filter the data. I'll describe in more detail below some of the optional method arguments and how they can be used to customize your map. But first, here are the required arguments:
215+
This function does a lot under the hood so you don't have to. It creates measures in the dataset so that bins will automatically adjust when filtering conditions change, it creates the map and adds text boxes for a legend and and a slicer to allow the user to filter the data. The method requires you to provide a shape file that corresponds to a variable in your dataset. For example, the dataset could have two columns: US states and average income. In this case you should provide a shapefile of US states.
216+
217+
The method also give two different options for binning the data and assigning ranges of values to different colors on the map. You can only specify one of the two options at a time. The first option is to provide a static set of breaks that you define yourself. This is ideal if you already know the structure of your data and how you want to assign ranges of values to different colors. Use the `static_bin_breaks` argument to pass an integer list of break values. The second option is to provide a list of percentiles that you want to use to bin the data into ranges. This option is intended to be used in conjunction with a third variable that you use to filter the data. For example, assume you have a third variable in your dataset of average income by state dataset, maybe age. You could pass age to the `filtering_var` argument to create a slicer attached to the map that filters the dataset by age before rerendering the map. You could then pass the list of percentiles to `percentile_bin_breaks` such as 0, 0.2, 0.4, 0.6, 0.8, 1. This will create breaks between 0% and 20%, 20% and 40%, etc and the bins will be recalculated each time the slicer filters are adjusted. So, for this example, if you selected "young people" in the slicer, the bins will be re-calculated and the map will re-render to show 20, 40, 60 etc, percentiles for just young people. If you provide `percentile_bin_breaks` you must also provide a `filtering_var`; the assumption being that if you don't need dynamic recalculation of bins based on the filtering variable, you'd be better off calculating a static set of breaks for yourself and passing them as a list to `static_bin_breaks`.
218+
219+
I'll describe in more detail below some of the optional method arguments and how they can be used to customize your map. But first, here are the required arguments:
220+
221+
1. `visual_id` - Give the button a unique id that you'll remember. This id cannot be the same as any other visual in the dashboard.
222+
2. `data_source` - The name of the dataset you want to use to build the map. This corresponds to the dataset_name field in the add data functions. You must have already loaded the data to the dashboard.
223+
3. `shape_file_path` - A path to a shapefile that you want to use to build the map. This file can be any valid shapefile accepted by power BI. In this example dashboard I use a geojson, but presumably an Arcgis file with a .shp extension would also work. This shape file will be added to the dashboard's registered resources.
224+
4. `map_title` - The title you want to put above the map.
225+
5. `location_var` - The name of the column in data_source that you want to use for the location variable on the map. This should also correspond to the geography of your shape file.
226+
6. color_var: The name of the column in data_source that you want to use for the color variable on the map. This variable should be numeric.
227+
filtering_var: str
228+
Optional. The name of a column in data source that you want to use to filter the color variable on the map. This must be supplied if providing percentile_bin_breaks. If you want to use percentiles without filtering (ie on static data), you should calculate the percentiles yourself and pass them to static_bin_breaks. Do not provide both static_bin_breaks and a filtering_var.
229+
static_bin_breaks: list
230+
This should be a list of numbers that you want to use to create bins in your data. There should be one more entry in the list than the number of bins you want and therefore the number of colors passed to the color_palette argument. The function will create bins between the first and second number, second and third, third and fourth, etc. A filtering_var cannot be provided if static_bin_breaks is provided. Use percentile bin breaks instead.
231+
color_palatte: list
232+
A list of hex codes to use to color your data. There should be one fewer than the number of entries in static_bin_breaks
233+
add_legend: bool
234+
True or False, would you like to add the default legend? (By default legend, I mean this function's default, not the Power BI default)
235+
static_bin_breaks: list
236+
This should be a list of numbers that you want to use to create bins in your data. There should be one more entry in the list than the number of bins you want and therefore the number of colors passed to the color_palette argument. The function will create bins between the first and second number, second and third, third and fourth, etc.
237+
percentile_bin_breaks: list
238+
This should be a list of percentiles between 0 and 1 that you want to us to create bins in your data. If provided, a filtering_var must also be provided. This will create power BI measures that dynamically update when the data is filtered by things such as slicers. There should be one more entry in the list than the number of bins you want and therefore the number of colors passed to the color_palette argument. Here's an example use case: to create 5 equal sized bins pass this list: [0,0.2,0.4,0.6,0.8,1]
239+
height: int
240+
Height of map on the page
241+
width: int
242+
Width of map on the page
243+
x_position: int
244+
The x coordinate of where you want to put the map on the page. Origin is page's top left corner.

src/powerbpy/page.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,13 +554,13 @@ def add_shape_map(self,
554554
data_source: str
555555
The name of the dataset you want to use to build the map. This corresponds to the dataset_name field in the add data functions. You must have already loaded the data to the dashboard.
556556
shape_file_path: str
557-
A path to a shapefile that you want to use to build the map. This shape file will be added to the registered resources.
557+
A path to a shapefile that you want to use to build the map. This file can be any valid shapefile accepted by power BI. In this example dashboard I use a geojson, but presumably an Arcgis file with a .shp extension would also work. This shape file will be added to the dashboard's registered resources.
558558
map_title: str
559559
The title you want to put above the map.
560560
location_var: str
561-
The name of the column in data_source that you want to use for the location variable on the map
561+
The name of the column in data_source that you want to use for the location variable on the map.This should also correspond to the geography of your shape file.
562562
color_var: str
563-
The name of the column in data_source that you want to use for the color variable on the map
563+
The name of the column in data_source that you want to use for the color variable on the map. This variable should be numeric.
564564
filtering_var: str
565565
Optional. The name of a column in data source that you want to use to filter the color variable on the map. This must be supplied if providing percentile_bin_breaks. If you want to use percentiles without filtering (ie on static data), you should calculate the percentiles yourself and pass them to static_bin_breaks. Do not provide both static_bin_breaks and a filtering_var.
566566
static_bin_breaks: list

src/powerbpy/shape_map.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ def __init__(self,
5353
data_source: str
5454
The name of the dataset you want to use to build the map. This corresponds to the dataset_name field in the add data functions. You must have already loaded the data to the dashboard.
5555
shape_file_path: str
56-
A path to a shapefile that you want to use to build the map. This shape file will be added to the registered resources.
56+
A path to a shapefile that you want to use to build the map. This file can be any valid shapefile accepted by power BI. In this example dashboard I use a geojson, but presumably an Arcgis file with a .shp extension would also work. This shape file will be added to the dashboard's registered resources.
5757
map_title: str
5858
The title you want to put above the map.
5959
location_var: str
60-
The name of the column in data_source that you want to use for the location variable on the map
60+
The name of the column in data_source that you want to use for the location variable on the map. This should also correspond to the geography of your shape file.
6161
color_var: str
62-
The name of the column in data_source that you want to use for the color variable on the map
62+
The name of the column in data_source that you want to use for the color variable on the map.This variable should be numeric.
6363
filtering_var: str
6464
Optional. The name of a column in data source that you want to use to filter the color variable on the map. This must be supplied if providing percentile_bin_breaks. If you want to use percentiles without filtering (ie on static data), you should calculate the percentiles yourself and pass them to static_bin_breaks. Do not provide both static_bin_breaks and a filtering_var.
6565
static_bin_breaks: list

0 commit comments

Comments
 (0)