Rails - Export multiple models to CSV - ruby

I am building a tool used for producing research reports. The tool produces reports with multiple models and nested models to display tabular data, and a report is structured something like this
Report
- main report attributes from report model
Nested Model 1
- nested model 1 attributes
Nested Model 2
- nested model 2 attributes
Right now I am exporting these reports to PDF, but I have gotten feature requests from users for exporting reports to Excel and CSV.
In my experience, CSV export has typically been good for "flat" associations, such as exporting a list of name/attributes from 1 model.
But if you have a complex report with multiple models, how do you structure an Excel / CSV file with data from multiple models?
Any examples of how you've structured something like this would be greatly appreciated.

If the attributes of the nested model you want to list in the CSV file are all has_one relation, it is not complicated that you can just append the nested model attributes right to the main model, report.
attr1, attr2, attr3,...,attrN, nested_attr1, nested_attr2...
For has_many relation, it is the similar solution but a little bit more complicated. First, you need to find the maximum number of the nested models belongs to a report. For example, if a report has many nested_reports, you need to check each report and use .count to find the number of the nested_reports and then choose the biggest one. Once you found the max number X, you can make a CSV like this:
attr1,...,attrN, nested_report1_attr1...nested_report1_attrN, nested_report2_attr1...nested_report2_attrN,...nested_reportX_attr1...nested_reportX_attrN
If a report only has one nested_report, the nested_report2...nested_reportX can just be left blank.
If the report has multiple nested models, then just append them as the above.
If you have a large number of data, finding the max number of nested model of the report will be a bottleneck. You may need an elaborated JOIN SQL to optimize it. However, I personally choose another solution: use counter_cache, so I can find that number as fast as possible by using:
Report.all.maximum(:nested_model_count)
You can check https://guides.rubyonrails.org/association_basics.html#options-for-belongs-to-counter-cache about how to use :counter_cache

Related

How to avoid circular dependency error in multiple calculated columns when deleting all data in data model?

Context:
I have a data model in Power pivot with three tables, tTasks, tCaseworks and tCaseworkStatus. I am attempting to create two calculated columns in tCaseworks which from the two data tables. All three tables are linked through the common field casework_id (see illustration below).
The data model is regularly updated with new data. The way I am doing this is as follows:
All three tables are sourced from three corresponding tables in my Excel workbook.
A VBA script deletes all records in the three Excel tables and then refreshes the data model (sidenote: because the data model demands lookup tables to not be empty the VBA code adds one row per table before refreshing).
New data is then added to the excel tables and the data model is refreshed.
This process works perfectly.
Problem:
The problem arises when I am adding calculated columns to tCaseworks and then attempting to update the data as described above. I have added two calculated columns; has_task and status_now. I am using the following DAX code:
has_task:
has_task =
IF (
CONTAINS (
RELATEDTABLE ( tTasks );
tTasks[casework_id]; tCaseworks[casework_id]
);
"Yes";
"No"
)
status_now:
status_now =
VAR TableX = RELATEDTABLE(tCaseworkStatus)
VAR ResultX = IF(
CONTAINS(TableX;tCaseworkStatus[casework_status_code];"Completed");"Completed";
IF(CONTAINS(TableX;tCaseworkStatus[casework_status_code];"Dismissed");"Dismissed";
IF(CONTAINS(TableX;tCaseworkStatus[casework_status_code];"Begun");"Begun";
IF(CONTAINS(TableX;tCaseworkStatus[casework_status_code];"Created");"Created";
"Find no status"))))
RETURN
ResultX
Both of these calculated columns work as expected as long as I do not delete the data in the model (I do have one hickup with both columns as described in this separated problem, but I think that is unrelated).
When the data has been deleted and I refresh the model I get the following error message:
"We cannot get the data from the data model. This is the error message we got: A circular dependency was discovered: 'tCaseworks'[status_now],'tCaseworks'[status_now],'tCaseworks'[has_task],'tCaseworks'[has_task],'tCaseworks'[status_now]."
Question:
What is creating this dependency and how can I avoid it?
My attempted solutions:
The problem only arise when there are two of these calculated columns. Any one of these two works perfectly without the other upon refreshing. I know that calculated columns are prone to circular problems, but unfortunately I need to use columns and not measures. I suspect that perhaps my choice in formula is creating the problem, most likely the contains-function. However, I don't know about any alternative ways of building the formulas I need. Any suggestions?
Edit:
I originally only posted a portion of my data model as I wanted the question to be as concise as possible but I guess it might have been confusing. The whole model concerns five objects from a case handling system: Claims, Cases, Caseworks, Tasks and Action Points. These objects are hierarchical, one claim can have one or more cases, but one case can only have one claim. Similarly, a case can have several caseworks, a casework can have several tasks, a task can have several action points. Additionally, the latter four can have a status attribute which is changed regularly.
I attempted to organize my data model in such a way that I had a lookup table for each object with unique values. I have many attributes for each object in my data that I did not include in the example above, and my goal was to add useful attributes through calculated columns in these tables. The data tables with the changes were intented to provide insight to the lookup tables.
I think your relationship model is a bit unusual. DAX works best when using something like dimensional fact model
I would consider the tCaseworkStatus a fact table since its like a log of the changes to your data. tTasks is a dimension, since it just add an extra dimension to your data.
The tCaseworks is not necessary since it doesn't hold any actual data (only calculated data).
if you want your current model to work, it might fix your problem if you just delete the relationship between tTasks and tCaseworks, and add a new between tTasks and tCaseworksStatus
edit.
it just occurred to me that the reason you have it like this, is that you may have a many-to-many relationship between tTasks and tCaseworksStatus. if that is the case you might have to create a proper many-to-many table. which is kind of what your tCaseworks is, but you cant have a relationship to the same key like you currently have.
edit2.
the solution seemed to be that somehow the Relatedtable function in conjunction with the relationship model was causing the error. using Lookupvalue instead seems to to have fixed the issue.

Can I create a (new) lookup table in Power Pivot by querying other tables in my data model?

Context:
I am creating a dashboard in Excel based on the data model I am building in Power Pivot. The source data in the data model is based on various other excel tables I am regularly receiving and copy-pasting into my workbook (their incoming structure is out of my control). My goal is to perform all data processing within Power Pivot/DAX rather than manipulating the data in the worksheets before loading into the model.
Problem:
In my model, I have a table (tabCases) which includes status updates on all cases from a management system. This table has a column named case-ID (not unique). I need to create a lookup-table with unique case-id's where I can create new columns with various KPIs for each case.
How can I do this in Power Pivot?
I found two suggestions in this article but none of them work for me (opt. 1 because it requires a manual creation of the unique ID list and opt. 2 because I don't have a database access).
In my mind there should be something really simple I could do, such as i.e.:
Add new table to data model
Set first column to be equal to DISTINCT(tabCases[caseID])
Is there such a way?
A Linkback Table might help you. Please see the link below:
https://www.sqlbi.com/articles/linkback-tables-in-powerpivot-for-excel-2013/
Thanks

How to access categories in Wix Database

I've been struggling to create a repeater which accesses specific information in my Wix data base. My question is a bit more complex than connecting the repeater to a column.
Rather, I want the repeater to access types of data within a column on the database. I have a column in the data base with an id, "category". For illustrative purposes, say the name of the data base is "store". I'm selling two different types of shirts. Some are casual, others formal. "category" has twenty iterations of both "formal" and "casual". If I create a repeater which accesses "category" and displays its text, I'll end up with a repeater forty iterations long. Instead, I want to parse out how many types of categories there are (in this case, two: "formal" and "casual") and only to display each category once-- a repeater which is only two iterations long.
(I know Wix has the ability of accessing how many types of information there are in column "category", I just don't know how to actualize on that ability. I know Wix has this ability because you can create a dynamic item page with a url of name "category". This will create a page for each category in "category". How do I do the same thing but for a repeater?)
One way to accomplish this is by using the Wix Data API Distinct query and then using the query result as the data for your repeater. This means you would not use the GUI connect to dataset, but the code in the page's IDE. Once the query returns, you can set the data property of the Repeater and then use the onItemReady() function for any further manipulation you may need.

Variance Column in BI Publisher Report Pivot Table

I have a data model using a private data source (not from a Subject Area), so I need to do this with a BI Publisher Report (not an Analysis).
In the data model I have a column called 'Financial Plan Type' that contains a few different values such as "Forecast", "Adjusted Budget", "Original Budget", etc.
I want to create a pivot table that pivots this column and then creates a variance column between "Adjusted Budget" and "Forecast" as example. Obviously I have an 'Amount' field in the table too.
It doesn't seem that I can do this directly in the report as the formulas and flexibility seem to be limited for the Reports (although I'm not 100% sure of this as I am fairly new to OBIEE), but I was thinking that I could adjust the data model to union in a variance amount or do something else with the data model to make this work. Does anyone have any ideas and/or best practices around doing this either in the data model or in the Report itself?
This going to be abstract, but you can do this in BIP or Analysis, depends on what the data source looks like.
If you have are able to compute the variance as an extra element in the datasource (might need to model it), then BIP RTF template designer does support Pivot tables. You might still need to add some XDO code in the loops.
If you are inclined to OBIEE, you can create your OWN data source on OBIEE. You will have to use the RPD data modeller if you are on OBIEE on premise, or write the transactional SQL if you are OTBI on the cloud.
Either way, the trick is to have the variance already computed in the XML, so BIP/OBIEE can simply print it off.

create views in filemaker

I am trying to make something like this in Filemaker Database views.
The database will contain employees and projects. There need to be two combined views. One displaying the list of employees and which projects they are on in which week and by which percentage. The other one displaying the list of projects and which employees are on it in which week by which percentage. For each employee and each project there need to be a sum of percentages for all assigned projects / employees.
I have made three tables in my database:
employee
projects
employee_on_projects
Now I want to create these views. Can anybody tell me how to do this from scratch?
Thanks in advance.
FileMaker is not a good tool for creating cross-tab reports, as it has no native functionality to build these. It has to be done manually. Do a search on cross tab reporting in FileMaker and you will get some ideas.
Traditionally you need relationships and fields to hold and display your data.
On the top of my head, in this case I would probably use merge variables for displaying progress and other calculated data to avoid having fields for all those weeks. A script would gather the data using ExecuteSQL and calculate the variables. The whole thing could even be generated in HTML and displayed in a web viewer.
This way you should get away without too many relationships and fields.
If you want a simpler, but more cluttered solution, you could go with regular fields for all 53 weeks and use a sub summary report. Each field could be set up with ExecuteSQL if possible, to avoid having a relationship for each period.

Resources