Forum Discussion
Hi jameshaid​,
You can use Python code to add the missing records via Custom Code Transformation. I have attached a notebook below that contains the Python code for adding missing weeks (based on week numbers) to charts.
Note that the community site doesn’t currently support .ipynb files. Please change the file extension from .txt to .ipynb after downloading it. For instructions on how to import and use a Custom Code notebook, please refer to the Custom Code Transformation documentation.
The notebook requires two inputs: Week Number Field Name and Break By Field Name. Use the names of the corresponding columns as they appear in the widget editor: the week number column for Week Number Field Name, and the Break By column (in your example, the column that contains KEHE, RAINFOREST DISTRIBUTION, and UNFI) for Break By Field Name.
Please see the screenshots below for reference. Let me know if you have any follow-up questions.
Custom Code configurations:
Result
- jameshaid09-01-2025Cloud Apps
Hello TriAnthony​ ,
Thank you for sending over this Notebook. It looks like it will be the solution. Unfortunately, the Sisense cloud app won't let me upload it even though I changed it from .txt to .ipynb (I even tried .sipynb, and that didn't work). I made the Import Notebook attempts from a Mac desktop and Mac laptop, and tried both dragging the file over and looking it up via Browse (Browse did not recognize it as a valid file for upload). Thoughts on what be preventing this or if there's another attempt that will work? I'm okay if you email it to me if passing it thru the Community might be the issue.
Regards,
Jim
- TriAnthony09-01-2025Admin
Hi jameshaid​,
I just sent the file to your registered email. If you're still unable to import the notebook, you can create a new one instead. In the "Create a new Notebook" window, give it a name and add two additional input parameters (located at the bottom of the window). All the other fields are optional.
Here are the Additional Input Parameters configurations:
WeekNumberFieldName parameter:
BreakByFieldName parameter:
Next, open the code editor (Jupyter Notebook) and delete all existing cells.
Paste the below code into the first empty cell:
# Test Cell # When the notebook is executed by the widget, this cell is ignored. # See the `Test Cell` section below for further details. from init_sisense import sisense_conn df_input = sisense_conn.get_debug_data() additional_parameters = '{\"WeekNumberFieldName\":\"Week Number\", \"BreakByFieldName\":\"Age Range\"}'
Then paste the below into the next cell:
import json import pandas as pd # Load the additional_parameters JSON string try: data = json.loads(additional_parameters) except json.JSONDecodeError as e: print(f"Failed to parse outer JSON: {e}") raise week_number_field_name = data["WeekNumberFieldName"] break_by_field_name = data["BreakByFieldName"] week_numbers = range(1, 53) # Get unique values for break_by_field_name break_by_field = df_input[break_by_field_name].unique() # Build full cartesian product of week_number_field_name and break_by_field_name full_index = pd.MultiIndex.from_product( [week_numbers, break_by_field], names=[week_number_field_name, break_by_field_name] ) # Rebuild the data frame df_result = ( df_input.set_index([week_number_field_name, break_by_field_name]) .reindex(full_index, fill_value=0) .reset_index() ) df_result
Save the code and switch back to the widget tab. Click Next, enter the names of your Week Number and Break By columns in the input fields. Click Done to close the window, then save the widget.