Forum Discussion
The_PBI_Chef
2 years agoRegular Visitor
Power BI developer mode (.pbip) merge conflict issues
I'm trying to implement version control in PBI using .pbip files, but I have an issue. When I create 2 different branches from main, and create one DAX measure in each of these branches, I can only m...
sausaged
1 year agoFrequent Visitor
Anonymous That's great to hear. Are you able to share a working version of this python script? This might be helpful for many users!
Anonymous
1 year agoNot applicable
sausaged Sure. Below is the version that works for us so far:
import json
import sys
def process(obj):
"""
Recursively sorts lists containing dictionaries with the 'name' attribute by the 'name' key,
and strips trailing \r from string values.
"""
if isinstance(obj, list):
# If all elements are dictionaries with a 'name' key, sort them
if all(isinstance(item, dict) and 'name' in item for item in obj):
# Sort the list of dictionaries by the 'name' key and recursively process each item in the list
return sorted([process(item) for item in obj], key=lambda x: x['name'])
else:
# Recursively process each item in the list
return [process(item) for item in obj]
elif isinstance(obj, dict):
# Recursively process each value in the dictionary
return {key: process(value) for key, value in obj.items()}
elif isinstance(obj, str):
# Strip trailing \r from strings
return obj.rstrip('\r')
else:
return obj
def main():
# Check if filename is provided as command-line argument
if len(sys.argv) < 2:
print("Usage: python reorder_content.py <file_name>")
sys.exit(1)
# Get filename from command-line argument
file_name = sys.argv[1]
try:
with open(file_name, 'r') as file:
content = json.load(file)
processed_content = process(content)
with open(file_name, 'w') as file:
json.dump(processed_content, file, indent=4)
print(f"Processed {file_name}")
except FileNotFoundError:
print(f"File not found: {file_name}")
except json.JSONDecodeError:
print(f"Error decoding JSON in file: {file_name}")
if __name__ == '__main__':
main()
The script should be located at `.\pre-commit\reorder-content.py` and triggered by `.\.git\hooks\pre-commit` content of which is as follows:
#!/bin/bash
FILE="./[Project Name].SemanticModel/model.bim"
python.exe ./pre-commit/reorder-content.py "$FILE"
git add "$FILE"
Don't forget to substitute [Project Name] respectively in the pre-commit script.